Bonjour !
Je ne comprend pas pourquoi mon programme ne peut pas lire sur un socket TCP, il me retourne l'erreur 10053.
nb: Mon pare-feu sous windows est desactivé!
Voici le "visuel" de l'interaction entre le client (a droite, et le serveur auquel il se connecte (a gauche)
Initialisation du serveur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 load_configuration(); welcome(); #if defined (WIN32) WSADATA WSAData; int error = WSAStartup(MAKEWORD(2,2), &WSAData); #else int error = 0; #endif int sock_err; if(error) { cout << "ERROR: WSAStartup" << endl; exit(0); } _server = socket(AF_INET, SOCK_STREAM, 0); if(_server == INVALID_SOCKET) { cout << "ERROR: INVALID_SOCKET" << endl; exit(0); } else { cout << "Socket is ready (TCP)" << endl; } sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_family = AF_INET; sin.sin_port = htons(_config["port"].ival); sock_err = bind(_server, (SOCKADDR*) &sin, sizeof(sin)); if(sock_err == SOCKET_ERROR) { cout << "ERROR: Binding socket" << endl; exit(0); } sock_err = listen(_server, 5); cout << "Socket is now listening on port " << _config["port"].ival << endl; FD_ZERO(&recvfs); pthread_create(&worker, NULL, Networker, this);
Quand un client se connecte:
Le thread qui qui les socket:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 int one = sizeof(sin); while(true) { _incomingsocket = accept(_server, (struct sockaddr*)&sin, &one); if(_incomingsocket == SOCKET_ERROR) { //SDL_Delay(50); continue; } string ip = string(); ip += lexical_cast<string>(static_cast<int>(sin.sin_addr.S_un.S_un_b.s_b1)); ip += "."; ip += lexical_cast<string>(static_cast<int>(sin.sin_addr.S_un.S_un_b.s_b2)); ip += "."; ip += lexical_cast<string>(static_cast<int>(sin.sin_addr.S_un.S_un_b.s_b3)); ip += "."; ip += lexical_cast<string>(static_cast<int>(sin.sin_addr.S_un.S_un_b.s_b4)); cout << "Client connected (IP: " << ip << "; sock: " << _incomingsocket << ")" << endl; FD_SET(_incomingsocket, &recvfs); register_session(create_session(ip, _incomingsocket)); if(_incomingsocket > _maxfd) { _maxfd = _incomingsocket; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 void* Networker(void* ptr) { ServerSocket* Master = reinterpret_cast<ServerSocket*>(ptr); int socketactivity; int sleep = 0, plength, i; uint16_t header; Packet packet; struct timeval tv; fd_set mask; Buffer = new char; int BytesReceived = 0; cout << "Thread Networker is now up and running" << endl; setup_callbacks(); cout << "Callbacks ready" << endl; bool shutdown = false; while(!shutdown) { if(select(Master->maxfd(), &Master->GetSocketSet(), NULL, NULL, NULL) < 0) { continue; // erreur ? } for(auto itr = Master->Clients().begin(); itr != Master->Clients().end(); ++itr) { if(!FD_ISSET(itr->second.GetSocket(), &Master->GetSocketSet())) { continue; // rien ne se passe } // recv: 5 premiers bytes Receive(itr->second.GetSocket(), 5); cout << "Received from " << itr->second.GetSocket() << ": (" << BytesReceived << ") " << Buffer << endl; plength = lexical_cast<int>(Buffer); cout << "packet length = " << plength << endl; if(plength == -1) { } else if(plength == 0) { Master->ClientDisconnection(itr->first); } else { // recv: packet Receive(itr->second.GetSocket(), plength); packet = Packet(Buffer); cout << "Received: " << Buffer << endl; clearBuffer(); // get header header = packet.ReadInt<uint8_t>(); // now, who is this socket ? if(itr->second.GetType() == sessiontype::unknown) { if(callback_unknown[header] != NULL) { callback_unknown[header](itr->second, packet); } } } FD_CLR(itr->second.GetSocket(), &Master->GetSocketSet()); } } } void Receive(const TCPsocket& sock, const int length) { clearBuffer(); BytesReceived = 0; int t = 0; cout << "Trying to receive on socket: " << sock << " ; l=" << length << endl; while (true) { if (BytesReceived == length) break; t = recv(sock, (Buffer + BytesReceived), length - BytesReceived, 0x00); cout << "received " << t << " bytes ; buffer= " << Buffer << endl; BytesReceived += t; if(t <= 0) { cout << "Error reading socket " << WSAGetLastError() << endl; break; } } } void clearBuffer() { Buffer = ""; }
Et coté client, voici connexion + envoie dun peu de données:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 int _temp; #if defined __MINGW32__ WSADATA _wsa; if (WSAStartup(MAKEWORD(2,2),&_wsa) < 0) { exit(EXIT_FAILURE); } #endif if ((_sock = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET) { exit(EXIT_FAILURE); } _saddr.sin_family=AF_INET; _saddr.sin_addr.s_addr=inet_addr(ip); _saddr.sin_port=htons(port); if(connect(_sock,(struct sockaddr*)&_saddr,sizeof(_saddr)) != 0) { cerr << "ERROR: Could not connect to " << ip << ":" << port << endl; exit(0); } cerr << "Connected to " << ip << endl; char* buf = new char; buf = "05 02 02\r\n"; send(_sock, buf, strlen(buf), 0x00);
Quelqu'un saurait m'expliquer d'où vient l'erreur ?
merci, nico
Partager