Handling multiple clients with TCP sockets in C?

Started by
4 comments, last by BeerNutts 12 years, 4 months ago
Hello,

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!

Thanks.
Advertisement
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.

http://www.linuxhowtos.org/C_C++/socket.htm

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

If you must write your server in C ...

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.
mark, Dan, thanks for the help.

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!

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.

Correct, that sums it up nicely.

Stephen M. Webb
Professional Free Software Developer


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.

http://www.linuxhowt..._C++/socket.htm


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.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement