Sunday, 15 January 2012

c - Set timeout for winsock recvfrom -


I am trying to set a blocking socket after trying 16 ms of recvfrom () on a port platform Windows I have seen so many examples online and it seems really simple that I can not seem to work for it, any help would be appreciated!

  #include & lt; Winsock2.h & gt; #include & lt; String & gt; #pragma Comment (lib, "ws2_32.lib") #define PORT_NUM 8001 int main (zero) {std :: string localIP; Sockaddr_in localAddr; Sockaddr_in Remote Edder; Host * localhost; Four buffers [1024]; WSADATA wsData; Favored Result = WSAStartup (MacWorld (2,2), & amp; wsData); // Winsock version 2 localhost = gatehostime (""); LocalIP = inet_ntoa (* (in_addr *) * localhost-> h_addr_list); LocalAddr.sin_family = AF_INET; LocalAddr.sin_port = htons (PORT_NUM); // set port number localAddr.sin_addr.s_addr = inet_addr (localIP.c_str ()); // Set IP address int mHandle = WSASocket (AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0); If (mHandle == INVALID_SOCKET) back 1; If (dam (mHandle, (SOCKADDR *) and local aid, size (local edge)) == SOCKET_ERROR 1 return; Timeline TV; Tv.tv_sec = 0; Tv.tv_usec = 1600; // Set timeout for RV call if settoopate (MHandle, SOL_COCK, SO_RCVTMOO, REINTERTP_CAST & lt; four * & gt; (& amp; TV), size (periodically) 1 return; inch length = psychof (remand Edd.); // Block here forever recvfrom (mHandle, buffer, 1024, 0, (SOCKADDR *) and remote adapter, and length); Return 0;} / * I also try to pass like time It is: int ms = 16; if (satochapet (mhindal, sol_cock, SO_rcvmeo, reinit_past & lt; four * & g T; (& amp; ms), size (int)) 1 returns; * /  

I looked at the selection ceremony and Laura said that I should and should get it to work smoothly thanks!

  fd_set fds; int n; structure timeline tv; / Set up / set the file descriptor set FD_Gero (and FDS); FD_SET (MHandle, and FDA); // Set up a straight time clock for Timetable Tv.tv_sec = 10; Tv.tv_usec = 0; // From time to time or Until the data is received Wait to N = select (mHandle, & amp; FDA, NULL, NULL, & amp; TV); If (n == 0) {printf ("timeout .. \ n"); Return 0; } And if (n == -1) {printf ("error .. \ n"); Return 1; } Int length = sizeof (RemoteAddad); Recvfrom (mHandle, buffer, 1024, 0, (SOCKADDR *) and remote adapter, and length);  

No comments:

Post a Comment