Handling multiple clients with TCP sockets in C?,
Members - Reputation: 101
Posted 28 November 2011 - 11:35 PM
I've lurked on gamedev for a while but never needed to post anything until now. My question is about reading from sockets with standard sockets in C.
Currently I'm building an online game for fun using UDP, but I want to change that since I don't want to handle ACKs and cases for lost packets etc.
So I'm looking to switch my server (written in C) to TCP. However, I'm confused as to how to handle multiple clients with TCP. I checked the FAQ and there is some useful info but I still have the same questions. Since TCP is stream-based, I can't use the packet model I had in mind with TCP. But, I want to be able to handle the TCP stream and abstract it so that the rest of my code will still see "packets" of data. The question is, with C sockets, when I do a read() or recv() from sockets, will data from multiple clients be intermixed? For example, if I do a read until I reach a "\n" character (to signify the end of a "packet"), can I assume all of it will be from one client? Maybe a better/cleaner solution with TCP is to have clients have their own sockets on separate ports?
Any help is appreciated. Thanks!
Crossbones+ - Reputation: 1719
Posted 29 November 2011 - 12:13 AM
Professional Programmer & Hobbyist Game Developer
Crossbones+ - Reputation: 1692
Posted 29 November 2011 - 02:34 AM
TCP gives you one socket per client. You'll need to read from all of them if you want to respond to requests from an arbitrary client.
Normally this is done by some kind of asynchronous polling loop. Because writing your own is a pain, most people use libevent or something similar.
It is also possible to use multiple processes or multiple threads, but for a game server it's likely to be a bad idea because you will want a large amount of shared data to be consistent.
I believe some commercial games use a "single process asychronous" design for each "zone" in the game, and have some mechanism of transparently handing off clients between them when a player moves from one zone to another. Of course this only works if the player controls a single entity or group of entities which are always in the same zone.
Members - Reputation: 101
Posted 29 November 2011 - 08:39 AM
I am actually using c++ for my server side app, but was using C for the sockets. Looks like with the libraries you posted, I can just use the listen() function and, within a loop, use the accept() call to accept all the new requests? Then I would create a client structure of some sort that had the socket and other necessary client info? If so, sounds easy enough. Thanks for the help!
Crossbones+ - Reputation: 8043
Posted 29 November 2011 - 09:38 AM
Correct, that sums it up nicely.
I am actually using c++ for my server side app, but was using C for the sockets. Looks like with the libraries you posted, I can just use the listen() function and, within a loop, use the accept() call to accept all the new requests? Then I would create a client structure of some sort that had the socket and other necessary client info? If so, sounds easy enough.
Professional Free Software Developer
Crossbones+ - Reputation: 4230
Posted 29 November 2011 - 01:15 PM
It's actually easier in my opinion. Simply put you create a listener socket and use that to handle incoming connection requests. Then you create a new socket for each individual connection. From there using the simple API's feeding in the socket (which is just an integral ID) you "talk" to each one directly. Running a simple google search for c socket tutorial will be your best start. However I highly recommend using the newer C++ technologies as they are just more stable and provide some more reliable power for high powered servers. Here is an excellent tutorial example to get you started with a C++ socket server on linux.
FYI, that link had nothing C++ specific about it; in fact, the title of that page is "C/C++ -> Sockets Tutorial. Add to it, all that code can be compiled by a C-only compiler.
---(Old Blog, still has good info): 2dGameMaking
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)