This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

strange select() and recvfrom() behaviour


Hello,

I observed some strange behaviour on Windows when I was using a select()
and recvfrom() combination to implement a socket listening behaviour with
timeout. Here is an extract from a test program.

struct timeval tv;

while(1)
{

   tv.tv_sec = 0;
   tv.tv_usec = 0.001*1000000;

   // The actual timeout is implemented with the select() function,
because the recvfrom() function can't do it.
   FD_ZERO(&rfds);
   FD_SET(sockfd, &rfds);
   retval = select(sockfd+1, &rfds, NULL, NULL, &tv);

   if (retval > 0)
   {
      // We received data from the socket.

      // read the data
      bytes_received = recvfrom(sockfd, buffer, MAXBUFLEN , 0, (struct
sockaddr *)&their_addr, &addr_len);

      printf("%d\t %d bytes received\n", retval, bytes_received);
   }
   else
   {
      // select() timed out and there was no data.
   }
}

First of all, you might notice that I'm trying to have select() time out
for 1 millisecond. This doesn't work, it times out for 10 ms and that
seems to be the lower limit. This issue was actually discussed recently.

The other thing is that after having sent a UDP packet of 6 bytes to the
program this is the output it produces:

1   6 bytes received
1   -1 bytes received
1   -1 bytes received
1   -1 bytes received
1   -1 bytes received
1   -1 bytes received
1   -1 bytes received

The first time select() returns correctly with 1 and I read all 6 bytes
from the socket. The buffer is large enough. After that select() returns 6
more times with 1 (clearly not a timeout!) and lies to me about the socket
being ready for ready, but recvfrom() sets it right with a return value of
-1;

The same thing does not happen in a pure linux environment with exactly
the same Code.

Marcell




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]