delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/02/14/05:33:55

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
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" <henryouly AT hotmail DOT com>
To: cygwin AT cygwin DOT com
Subject: Socket problem after fork()
Date: Fri, 14 Feb 2003 10:33:23 +0000
Mime-Version: 1.0
Message-ID: <F58BHr5mzoy2n8UziQN000246d8@hotmail.com>
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 <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019