X-Recipient: archive-cygwin AT delorie DOT com X-Spam-Check-By: sourceware.org X-IAI-Env-From: : [131.220.8.20] Message-ID: <46567.131.220.7.1.1193238723.squirrel@webmail.iai.uni-bonn.de> Date: Wed, 24 Oct 2007 17:12:03 +0200 (MEST) Subject: strange select() and recvfrom() behaviour From: "Marcell Missura" To: cygwin AT cygwin DOT com User-Agent: SquirrelMail/1.4.5 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com 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/