![]() |
| | #1 |
| Registered User Join Date: Sep 2004
Posts: 12
| Using fork() to accept multiple clients 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 | |
| | #2 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| Moved to Networking/Device Communication forum. |
| Thantos is offline | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |