This is the mail archive of the cygwin@cygwin.com 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]

Socket problem after fork()


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/


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