X-Recipient: archive-cygwin AT delorie DOT com X-Spam-Check-By: sourceware.org X-IAI-Env-From: : [131.220.8.20] Message-ID: <43152.131.220.7.1.1193247097.squirrel@webmail.iai.uni-bonn.de> Date: Wed, 24 Oct 2007 19:31:37 +0200 (MEST) Subject: Re: 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 Hi, actually you were right, I was a bit too quick with my extract. It didn't contain an important line. I'm also sending out stuff through that socket. So here it goes, you can copy paste and compile this. #include #include #include #include #include #include #include #include #include #include #include #define MYPORT 8080 // The default port the server will listen on. #define MAXBUFLEN 70 int main(int argc, char *argv[]) { int sockfd; int port = MYPORT; struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information int bytes_received; char buffer[MAXBUFLEN]; struct timeval tv; fd_set rfds; int retval; socklen_t addr_len = sizeof(their_addr); // open the UDP socket and bind it my_addr.sin_family = AF_INET; my_addr.sin_port = htons(port); my_addr.sin_addr.s_addr = INADDR_ANY; memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero)); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Could not open socket"); exit(1); } if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof (my_addr)) == -1) { perror("Could not bind socket."); exit(1); } printf("Server is ready and listening on port %d\n", ntohs(my_addr.sin_port)); while (1) { tv.tv_sec = 0; tv.tv_usec = 0.01*1000000; FD_ZERO(&rfds); FD_SET(sockfd, &rfds); retval = select(sockfd+1, &rfds, NULL, NULL, &tv); if (retval > 0) { // We received data from the socket. bytes_received = recvfrom(sockfd, buffer, MAXBUFLEN , 0, (struct sockaddr *)&their_addr, &addr_len); printf("retval:%d\t%d bytes received.\n", retval, bytes_received); } else { // select() timed out sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&their_addr, sizeof their_addr); } } // can't be reached, the OS will take care of it anyways, but oh well... close(sockfd); return 0; } Now if you compile it in cygwin and let it run on a windows machine and send it a UDP packet you will see that select() keeps returning with 1 even though the socket has no data to read. Do the same on linux and it works like it should, select() returns 1 only if there is actually incoming data to read. 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/