-1

I am porting a socket based application from Linux to Windows CE 6.0. I came across a line of code which was setting the socket option for Receive Timeout.

struct timeval timeout = 200; timeout.tv_usec = 200000; setsockopt(mySock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, (socklen_t) sizeof(timeout));

I searched for possible porting implementations and could find these two threads relevant. setsockopt() with RCVTIMEO is not working in windows mobile5 and Set timeout for winsock recvfrom

After setting the receive timeout as 200ms, there is a call to recv() for receiving data from the remote IP(sender). The first link explained clearly to spawn off a thread and wait on it but 200ms looked too less for my case as the sender sends for ~10 sec. The second link's select() suggestion is what I added to my code but the behavior is very inconsistent. Sometimes it receives no packets, sometimes 1 or sometimes more. But the existing implementation works fine on Linux.

Am I doing the porting right way? Can anyone point out a probable mistake or make a suggestion?

Thanks!

Community
  • 1
  • 1
deepak_
  • 1
  • 2

1 Answers1

0

I think the "select()" suggestion to port your linux code is the right one.

I would use the following code:

struct timeval tmout;

#ifdef LINUX
//...
#else
while (true)
{

           struct fd_set fds;
           // Set up the file descriptor set.
           FD_ZERO(&fds) ;
           FD_SET(mySock, &fds) ;

           // Set up the struct timeval for the timeout.
           tmout.tv_sec = 0 ;
           tmout.tv_usec = 200000 ;

           // Wait until timeout or data received.

           n = select ( NULL, &fds, NULL, NULL, &tmout ) ;

           if ( n == 0)
           { 
             printf("select() Timeout..\n");
             //return 0 ;
           }
           else if( n == SOCKET_ERROR )
           {
             printf("select() Error!! %d\n", WSAGetLastError());
             //return 1;   
           }
           else
               printf("select() returns %d\n", n);
}
#endif

I'm running the same code in a WCE6 app and it is working fine for me. If you are executing this code in a loop and your sender is sending every 10 seconds you should see every 10 seconds the select returning n>0.

Hope this is helpful

salvolds
  • 201
  • 2
  • 14