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 Message-Id: <200302121606.h1CG6OJ28157@zsulink.zsu.edu.cn> From: "Henry" To: "cygwin AT cygwin DOT com" Subject: Problem with socket in gcc programming Mime-Version: 1.0 Content-Type: text/plain; charset="GB2312" Content-Transfer-Encoding: 7bit Date: Thu, 13 Feb 2003 0:2:43 +0800 X-Filter-Version: 1.7 (fw.zsu.edu.cn) This is a demo program to issue my problem i'm facing to. #include #include #include int main(int argc, char *argv[]) { int sockfd, accefd, rsinlen, on = 1; pid_t pid; struct sockaddr_in sin, rsin; unsigned long waittime; if (fork()) exit(0); 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(80); sin.sin_addr.s_addr = INADDR_ANY; bind(sockfd, (struct sockaddr *)&sin, sizeof(struct sockaddr)); listen(sockfd, 256); while (1) { accefd = accept(sockfd, (struct sockaddr *)&rsin, &rsinlen); if (accefd >= 0) { pid = fork(); if (pid < 0) { printf("Parent: fork error\n"); close(accefd); continue; } if (pid == 0) { close(sockfd); //close(accefd); /* in my test, althought I add this line the second time, it ran into the same situation */ process(accefd); printf("Child return to parent\n"); close(accefd); exit(0); } else { waitpid(pid, NULL, 0); printf("Parent exiting\n"); close(accefd); exit(0); } } } return 0; } int process(int fd) { int i; printf("Child start\n"); close(fd); printf("fd %d closed\n", fd); if (fork() > 0) return 0; printf("in grandchild, infinite loop\n"); while (1) ; } While I compile and run this program in cygwin with gcc, and then telnet to 80 port, my telnet client can't disconnect and I have to use ^] to terminate. The same program tested in FreeBSD 5.0RC2, and got the following result. su-2.05b# ./test su-2.05b# telnet localhost 80 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Child start accept fd closed Child return to parent Parent exiting in grandchild, infinite loop Connection closed by foreign host. The socket was closed automatively. I'm wondering whether I dismissed something or this is a bug in cygwin. I spent hours in google and find many bugs description, but none of them is familiar with my problem. My cygwin version is 2.194.2.24. -- 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/