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]

Problem with socket in gcc programming


This is a demo program to issue my problem i'm facing to.
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>

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/


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