Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm 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 X-Originating-IP: [211.167.0.131] From: "Henry Richard" To: cygwin AT cygwin DOT com Subject: Socket problem after fork() Date: Fri, 14 Feb 2003 10:33:23 +0000 Mime-Version: 1.0 Content-Type: text/plain; charset=gb2312; format=flowed Message-ID: X-OriginalArrivalTime: 14 Feb 2003 10:33:23.0597 (UTC) FILETIME=[8040F3D0:01C2D414] Note-from-DJ: This may be spam Hi all~ This time I upgrade my cygwin version to 1.3.20 and using gcc 3.2. The program I submit last time does work in this new environment. How ever, I have found that fork() can't be executed twice before closing the socket, otherwise the telnet client will be blocked unless giving it a ^]. I think this problem is also caused by Winsock2 in Windows 2000 system, according to Microsoft KB. "Windows Sockets version 2.0 does not deallocate a socket that has been duplicated [using WSADuplicateSocket()] if the Closesocket() function is called against the duplicated socket descriptor first, and then against the duplicate socket. Although the socket is ultimately deallocated when the process quits, overuse of socket resources may occur in the interim. Even after closing the socket at the program level, the socket provider may see a socket using that address." How ever, I'm familiar with neither unix kernel nor WINAPI. I'm not able do research in the source of cygwin to approve anything. The following is a test case. Try to let "loop" be a number greater than 1 (i.e. 2). Then telnet localhost in DOS box. /* test case in cygwin 1.3.20/Windows 2000 Professional 5.00.2195 Chinese version/gcc 3.2-3/Intel 166MMX*/ #include #include #include #include int main(int argc, char *argv[]) { int sockfd, accefd, rsinlen, on = 1, i, loop; pid_t pid; struct sockaddr_in sin, rsin; sockfd = socket(PF_INET, SOCK_STREAM, 0); setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); memset(&sin, 0, sizeof(struct sockaddr)); rsinlen = sizeof(struct sockaddr); sin.sin_family = AF_INET; sin.sin_port = htons(23); sin.sin_addr.s_addr = INADDR_ANY; bind(sockfd, (struct sockaddr *)&sin, sizeof(struct sockaddr)); listen(sockfd, 256); accefd = accept(sockfd, (struct sockaddr *)&rsin, &rsinlen); if (accefd >= 0) { loop=2;/* if loop is greater than 1, socket won't be closed */ for (i = 0; i < loop; i++) { pid = fork(); if (pid > 0) { close(accefd); return 0; } } close(accefd); sleep(100000); } } _________________________________________________________________ 与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Bug reporting: http://cygwin.com/bugs.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/