Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionint optval = 0x10; /* 16 millisecondes exprès pour qu'il est time out*/ setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&optval, sizeof(optval));
BSD options not supported for setsockopt are shown in the following table. Value Type Meaning SO_ACCEPTCONN BOOL Socket is listening. SO_RCVLOWAT int Receives low watermark. SO_RCVTIMEO int Receives time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2). SO_SNDLOWAT int Sends low watermark. SO_SNDTIMEO int Sends time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2). SO_TYPE int Type of the socket.
... SO_SNDTIMEO lit la valeur de timeout en emission (seulement en lecture) SO_RCVTIMEO lit la valeur de timeout en reception (seulement en lecture) ...
#include <winsock2.h> #pragma comment(lib, "ws2_32.lib") ... char message[BUFFER_LENGTH]; SOCKET skt_client; SOCKADDR_IN sin_client; int optval = 5000; // Timeout 5 sec ... // Socket creation sin_client.sin_addr.s_addr = inet_addr(..);sin_client.sin_family AF_INET; sin_client.sin_port htons(..); skt_client = socket(AF_INET, SOCK_STREAM, 0); if(skt_client == INVALID_SOCKET) throw string("ERROR"); setsockopt(skt_client, SOL_SOCKET, SO_RCVTIMEO, (char*)&optval, sizeof(optval)); setsockopt(skt_client, SOL_SOCKET, SO_SNDTIMEO, (char*)&optval, sizeof(optval)); // Socket connection if(connect(skt_client, (SOCKADDR *)&sin_client, sizeof(sin_client)) != SOCKET_ERROR) { message[0] = 0x15; message[1] = 0x66; message[2] = 0x00; message[3] = 0x78; message[4] = 0x50; if(send(skt_client, message, 5, 0) == SOCKET_ERROR) throw string("ERROR"); if(recv(skt_client, message, BUFFER_LENGTH, 0) != SOCKET_ERROR) ... }