From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,43dbd14f05580f2f,start X-Google-Attributes: gid1164f8,public X-Google-ArrivalTime: 1993-07-10 17:40:53 PST Newsgroups: comp.sys.dec Path: gmd.de!Germany.EU.net!news.dfn.de!darwin.sura.net!spool.mu.edu!torn!watserv2.uwaterloo.ca!watserv1!zeus!bscanlan From: bscanlan@zeus (Bruce D. Scanlan) Subject: Ultrix sockets. Usefull? Message-ID: Sender: news@watserv1.uwaterloo.ca Organization: University of Waterloo X-Newsreader: TIN [version 1.1 PL8] Date: Sat, 10 Jul 1993 23:58:04 GMT [ Article crossposted from comp.unix.ultrix ] [ Author was Don A. Bowman ] [ Posted on Sat, 10 Jul 1993 23:21:39 GMT ] I have a program that wishes to be a server. Unfortunately, it seems to close the child's socket in the parent. ie: sock = ... if (!fork()) { doit(sock); } close(sock); doit(int sock) { ... close(sock); } the problem is, the socket gets closed before the child process can deal with it. My Internetworking with TCP/IP Vol I says that a reference count is maintained within the sockets. In this fashion, the reference is incremented when a child is made, and decremented for every close. When the reference hits 0, the socket is closed. My question: a) Do you know what I am doing wrong? (A longer example, compilable, is included at the end of this message). b) How would you do this? I need the fork since this server will be serving large files. c) Where would you go, or how would you look, for help? Appendix: Example source code ------------------------------------------------------------------------ #include #include #include #include #include #define SERV_UDP_PORT 6543 #define SERV_TCP_PORT 6543 #define SERVER_HOST_ADDR "129.97.50.128" int main(int argc, char **argv) { int sockfd, newsockfd, clilen, childpid; struct sockaddr_in cli_addr, serv_addr; if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("server: "); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(SERV_TCP_PORT); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("bind: "); exit(0); } listen(sockfd, 5); for ( ; ; ) { clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); if (newsockfd < 0) { perror("newsockfd"); exit(0); } if (( childpid = fork()) < 0) { perror("FORK"); exit(0); } else if (childpid == 0 ) { close(sockfd); str_echo(newsockfd); exit(0); } close(newsockfd); } } str_echo(int fd) { int c; printf("In str_echo\n"); while ( (c = fgetc(fd)) != '*') { fputc(stdout,c); fflush(stdout); } printf("Done str_echo\n"); } ------------------------------------------------------------------------ If I put a sleep before the close(newsockfd), the connection dies that time later. It shouldn't die until the child kills it. -- Don Bowman (519) 744-6154 dbowman@zeus.uwaterloo.ca -- -------------------------------------------------------------------------- Bruce Scanlan | Undergrad, Systems Design Engineering bscanlan@zeus.uwaterloo.ca | University of Waterloo | Waterloo, Ontario, Canada