Problem with tcp server

Discussion in 'C Programming' started by yumos, Jan 29, 2010.

  1. yumos

    yumos

    Joined:
    Jan 29, 2010
    Messages:
    1
    Hello @ all,

    I hope you can give me some advice :damnmate:

    I will be following code for a tcp server and doStuff () function, the
    clients treated. From some point, I have several identical
    clients (zombies, I think), the same records in the database
    write. Has anyone an explanation? What can I do?

    int main(int argc, char *argv[])
    {
    int srvrSock; /* Socket descriptor for server */
    int clntSock; /* Socket descriptor for client */
    struct sockaddr_in echoServAddr; /* Local address */
    struct sockaddr_in echoClntAddr; /* Client address */
    unsigned short echoServPort; /* Server port */
    unsigned int cliAddrLen; /* Length of incoming message */
    int pid;
    struct sigaction sa;
    int loop;

    char ip[16];

    /* Test for correct number of parameters */
    if (argc < 2) {
    fprintf(stderr,"Verwendung: %s <TCP SERVER PORT>\n", argv[0]);
    exit(1);
    }
    //end if

    /* First arg: local port */
    echoServPort = atoi(argv[1]);

    /* Create socket for sending/receiving */
    if ((srvrSock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    DieWithError("socket() failed");
    //end if

    /* Construct local address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out
    structure */
    echoServAddr.sin_family = AF_INET; /* Internet
    address family */
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming
    interface */
    echoServAddr.sin_port = htons(echoServPort); /* Local port */

    /* Bind to the local address */
    if (bind(srvrSock, (struct sockaddr *) &echoServAddr,
    sizeof(echoServAddr)) < 0)
    DieWithError("ERROR on binding");
    //end if

    /* Listen */
    if (listen(srvrSock,5)<0)
    DieWithError("listen() failed");
    //end if

    sa.sa_handler = sigchld_handler; // reap all dead processes
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
    perror("sigaction");
    exit(1);
    }


    /* Set the size of the in-out parameter */
    cliAddrLen = sizeof(echoClntAddr);

    while (1) {
    printf(" loop %d \n\n", loop);
    loop++;

    /* Wait for a client to connect */
    if ((clntSock = accept(srvrSock, (struct sockaddr *)
    &echoClntAddr, &cliAddrLen)) < 0)
    DieWithError("accept() failed");
    //end if

    /* clntSock is connected to a client! */
    strcpy(ip,inet_ntoa(echoClntAddr.sin_addr));
    printf("Handling client %s\n", ip);

    pid = fork();
    if (pid < 0)
    {
    DieWithError("ERROR on fork");
    }
    else if (pid == 0)
    {
    close(srvrSock);
    dostuff(clntSock,ip);
    close(clntSock);
    exit(0);
    }
    else close(clntSock);

    } /* end of while */

    return 0; /* we never get here */
    }

    --

    Best regards
    Burak Senel
    yumos, Jan 29, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xchris
    Replies:
    5
    Views:
    4,114
    nobull@mail.com
    Jun 22, 2004
  2. LeyvaOne
    Replies:
    0
    Views:
    624
    LeyvaOne
    May 31, 2005
  3. GarDavis
    Replies:
    0
    Views:
    5,067
    GarDavis
    Nov 16, 2005
  4. Wonderer
    Replies:
    9
    Views:
    5,012
    Roedy Green
    Apr 26, 2004
  5. Tiger
    Replies:
    5
    Views:
    952
    Dave Thompson
    May 1, 2006
Loading...

Share This Page