Jump to content

  • Log In with Google      Sign In   
  • Create Account

Handling multiple clients with TCP sockets in C?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 barney1   Members   -  Reputation: 101

Like
0Likes
Like

Posted 28 November 2011 - 11:35 PM

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.

Sponsor:

#2 Dan Mayor   Crossbones+   -  Reputation: 1712

Like
0Likes
Like

Posted 29 November 2011 - 12:13 AM

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

Digivance Game Studios Founder:

Dan Mayor - Dan@Digivance.com
 www.Digivance.com


#3 markr   Crossbones+   -  Reputation: 1653

Like
0Likes
Like

Posted 29 November 2011 - 02:34 AM

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.

#4 barney1   Members   -  Reputation: 101

Like
0Likes
Like

Posted 29 November 2011 - 08:39 AM

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!

#5 Bregma   Crossbones+   -  Reputation: 5182

Like
0Likes
Like

Posted 29 November 2011 - 09:38 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.

Correct, that sums it up nicely.
Stephen M. Webb
Professional Free Software Developer

#6 BeerNutts   Crossbones+   -  Reputation: 2950

Like
0Likes
Like

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.

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)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS