Using fork() to accept multiple clients

This is a discussion on Using fork() to accept multiple clients within the Networking/Device Communication forums, part of the General Programming Boards category; I've already written a simple chat client, and now I'm trying to write a server that will accept messages from ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    12

    Using fork() to accept multiple clients

    I've already written a simple chat client, and now I'm trying to write a server that will accept messages from multiple clients at once (using the fork() command). I've been reading up on it and have tried the following code in my main, but even after a client is opened the server just sits there waiting for a connection. Am I missing something obvious?

    Code:
    main(int argc, char *argv[]) {
    int s = MakeServerSocket(7654);
    while (1) {
          struct sockaddr_in sa;
          int sa_len = sizeof(sa);
          int fd = accept(s, (struct sockaddr *) &sa, (unsigned int *)&sa_len);
          errorCheck(fd,"No connection");
          int pid = fork();
          errorCheck(pid, "Forking error");           //error if pid == -1
          if (pid == 0) {                                        //child process
               close(s);                                           //close original listening socket
               chat(fd);                                           //read/write from client
               close(fd);                                          //close socket
               exit(1);
              }
          if (pid > 0) {
                int x; 
                waitpid(-1,&x,WNOHANG);               //waits 'till child has exited
                close(fd);                                         //closes parent
              }
    
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Moved to Networking/Device Communication forum.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    You haven't created a socket, bind()ed to a port, or set up a listen()ing queue. You seem to be new to network programming. Check out http://www.ecst.csuchico.edu/~beej/guide/net/html/ for a nice tutorial.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    12
    My bad, I should've specified, the binding and listening is all in the seperate MakeServerSocket method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 12:04 AM
  2. TCP Sockets: multiple clients - one server
    By Printisor in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 11:34 AM
  3. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. Replies: 2
    Last Post: 03-05-2002, 05:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21