C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 02-25-2005, 01:31 PM   #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
          }

}
Lone is offline   Reply With Quote
Old 02-25-2005, 01:36 PM   #2
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Moved to Networking/Device Communication forum.
Thantos is offline   Reply With Quote
Old 03-05-2005, 10:47 AM   #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.
Yasir_Malik is offline   Reply With Quote
Old 03-17-2005, 10:07 AM   #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.
Lone is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple clients to "internet" server Zarniwoop C Programming 2 10-11-2008 11:04 PM
TCP Sockets: multiple clients - one server Printisor C Programming 4 11-01-2007 10:34 AM
Socket Help - Multiple Clients project95talon C Programming 5 11-17-2005 02:51 AM
fork(), exit() - few questions! s3t3c C Programming 10 11-30-2004 06:58 AM
More Winsock threads: Proper way to recv() and send() to multiple clients Unregistered Windows Programming 2 03-05-2002 05:52 AM


All times are GMT -6. The time now is 01:31 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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