April 28th, 2003, 08:29 PM
I'm lost while I'm writing a socket network program. My program is working for a single client. But I have no idea how to change my program to works with more than one client. Can anyone give me solution? thanks a lot.
April 28th, 2003, 08:50 PM
i am new to this myself, but perhaps i can help. first off though:
is it tcp/ip or udp? if it is UDP, u can get away with a singlethreaded server and a bunch of clients since they dont actually establish a connection. UDP just sends packets back and forth. if it is tcp/ip, i believe u need a multi threaded server to handle more than one client, since a constant connection is being established.
April 29th, 2003, 10:08 AM
I'm assuming that you're writing a TCP server. Multithreading is one option and about your only option if you're in Windows. If you're doing in Linux/UNIX and are not comfortable with pthreads, you could consider forking a new process.
Either way, the main process/thread is doing the listening. When it accepts a connect, then it can create a new thread or process that will handle the client connected to it and finally return to listening for the next connect to come in.
There's an example of a multithreaded TCP server at the site for "The Pocket Guide to TCP/IP Sockets: C Version" by Michael J. Donahoo and Kenneth L. Calvert at http://cs.baylor.edu/~donahoo/PocketSocket/ . Of course, they're using pthreads, but the concepts are pretty much the same under Windows. Actually, they also have Winsock versions of their examples.