How does video game networking work?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

acerookie1
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 48
Joined: Fri Feb 12, 2010 12:46 pm
Favorite Gaming Platforms: PC, Wii, SNES, GameCube
Programming Language of Choice: C++

How does video game networking work?

Post by acerookie1 »

how does the server and the client work? what are sockets? UDP or TCP?
Last edited by acerookie1 on Thu Jul 01, 2010 6:05 pm, edited 2 times in total.
Image
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: How does video game networking work?

Post by K-Bal »

User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: How does video game networking work?

Post by dandymcgee »

acerookie1 wrote:UPC or TCP?
I think "UDP or TCP" will yield far more results. A UPC (universal product code) is the bar code you'd find located on just about every item sold in stores today. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: How does video game networking work?

Post by Milch »

I'm currently writing on my multiplayer part of my engine - and as far as I can tell you - its a pain in the ass.
So UDP or TCP?
If you want to make somekind of a chat or anything else that can be slow - use TCP.
If you want to make a game - and I mean games like CSS, Wc3, ... - use UDP.

Client Server model:

On most modern games its something like this:
Server holds all the information and is more less 'god'
Clients just hold the data that they need to know to render the stuff what they see.
The client then asks the server something like "Can I go forward?" and the server tells them "yes you can/can't!" and sents them the new position.
But it would feel horrible this way, so the client runs the same logic as the server and "predicts" its own movement.
And use the link K-Bal posted - that is also my main resource.

And: I would advise you to use a crossplattform lib for the sockets, so you dont have to write your socket system for each OS.
Follow me on twitter!
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: How does video game networking work?

Post by K-Bal »

For UDP vs. TCP I have to say:

I'm using TCP for my action-game and it feels good. I tested it with a friend (Ping: ~30ms) and it's not laggy at all and I've not even implemented lag compensation. You don't have to check user ids since you have one connection for each user, too. Furthermore, if you need messages to arrive and to arrive in order, then you will see yourself implementing what TCP already has available with no extra work.

I gave TCP a chance because I am lazy and it's turning out well so far ;)
noob
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 10
Joined: Wed Oct 29, 2008 1:41 pm

Re: How does video game networking work?

Post by noob »

UDP is consider connectionless because it just sends data and does not do any error checking. TCP will establish a connection and send data with error checking. TCP is not be as fast as UDP because of the over head of creating a connection and resending any lost packets. The choice between UDP and TCP comes down to if you can afford to drop packets or not. Thats what I learned in networking anyway.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: How does video game networking work?

Post by wearymemory »

noob wrote:The choice between UDP and TCP comes down to if you can afford to drop packets or not.
QFE.

UDP is something I would implement for a streaming media or a multicasting (this can include Lan games) application, but I would think twice before using it in most games, and as far as I'm concerned, the overhead is negligible in most game development environments. If I ended up making a game which implemented the UDP protocol, then I would probably end up writing a partially reliable protocol on top of it because datagram reordering/dropping could interfere with the game's logic, so I wouldn't say that using UDP is any easier than using TCP.

As for the structure of data sent to and from the applications, most games format the data with a header. Usually the first two bytes are the length of the data, and then next two bytes are the type (which is used to distinguish the packets from each other, from which the application will know which offsets contain certain data). The data sent to the server from a client notifying it of a movement could be structured into a byte array like so:

Code: Select all

Offset    Type        Value
0         short       10           // Length specifies the number of bytes in this net message.
2         short       5            // Type identifies the following net message as containing a user's movement information.
4         int         UserID
8         short       Direction
The data is often written and read using the Little Endian byte order.
Of course, most games will implement a cipher for encrypting and decrypting data when it is sent and received.

If you are interested in speed or scalability, then let's talk about server threading models.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: How does video game networking work?

Post by avansc »

myeah.

there may be some games that get a way with TCP. chess, boggle, maybe SOME MMO's.

but if you look at any top notch FPS, its UDP. TCP will will kill you if you get in a situation where the server or client looses a packet and does not know how to send it again. your connection times out effectivly.

UDP does not give a shit, its just goes on. now do your self a test. write a few UDP clients that talk with a server that send numbers in sequential order, then in the end you can easily calculate the packets lost, it will be so few IF any on a small scale, that there is just no contest.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: How does video game networking work?

Post by K-Bal »

avansc wrote:TCP will will kill you if you get in a situation where the server or client looses a packet and does not know how to send it again. your connection times out effectivly.
Resends are done automatically with TCP.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: How does video game networking work?

Post by avansc »

Yes and? Like tcp never times out?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: How does video game networking work?

Post by K-Bal »

avansc wrote:Yes and? Like tcp never times out?
No. Are you talking about congestion? If that is the case, I don't see why UDP would be better in that case. Yes, the connection would not time out (since there is no connection) but the game will propably run like shit.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: How does video game networking work?

Post by wearymemory »

I can only see the result of this topic being a stalemate. Both protocols have their own practical uses and TCP shouldn't always be ruled out because there are plenty of popular MMO's that show good examples of TCP's capabilities. However, when it comes to 3D action games like first person shooters, then smooth gameplay and speed is likely to be more important than correctness, but developers will sometimes find themselves writing or using wrappers that offer TCP-like correctness over a UDP protocol. Either way, an effective socket system could rule out issues like TCP timeouts or dropped and rearranged datagrams.

Like K-Bal mentioned, it depends on what your project is. A lot of games don't necessarily need to use UDP, but you could always make provisions for it to be implemented later on in your game's development. For the more "top-notch" action games that rely on sending data quickly (spamming a bunch of movement/view packets), and not always correctly, you might want to use UDP. If you're not aiming to create games like CSS or Wc3 for a while, then you might not need to worry about UDP right now, but know that it's an option. The page behind this magical text may help get you started.
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: How does video game networking work?

Post by Live-Dimension »

In the end, you'd (hopefullly) write a wrapper class around whatever you are using. So a network class, with send/recieve functions and whatever else. That way, its not too much work to add udp/tcp later on. All you'd have to do is change the class, the connection details, and maybe a little bit in some other areas.
Image
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: How does video game networking work?

Post by MrDeathNote »

Live-Dimension wrote:In the end, you'd (hopefullly) write a wrapper class around whatever you are using. So a network class, with send/recieve functions and whatever else. That way, its not too much work to add udp/tcp later on. All you'd have to do is change the class, the connection details, and maybe a little bit in some other areas.
Your right about that, thats pretty much the way i developed the networked air hockey game. It wouldn't have been too much work to switch it either way.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: How does video game networking work?

Post by Zer0XoL »

Just a little kind of offtopic note, ive managed to use placement new on my own structs and send them over the network, i dont know if this is a new usage, but it works and is simple, idk if its fast or secure, it stores the new struct in a char array buffer and then it sends it and then it recievs and then you can get it out of the buffer again :)
Image
Im Blue
Post Reply