Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: bscanlan@zeus (Bruce D. Scanlan)
Subject: Ultrix sockets. Usefull?
Date: Sat, 10 Jul 1993 23:58:04 GMT	[thread overview]
Message-ID: <C9z3wt.B1L@watserv1.uwaterloo.ca> (raw)

[ 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 <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#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               


                 reply	other threads:[~1993-07-10 23:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox