Networking

October 1, 2008

UDP vs. TCP

What is the best way to send data between machines? Do you use TCP sockets, UDP sockets or a mixture of both? In this article we explore this fundamental choice and decide on the best option for fast-paced action games.

Sending and Receiving Packets

This article shows how to send and receive UDP packets using BSD sockets, giving you source code that works across MacOS X, Windows and Unix.

Virtual Connection over UDP

Learn how to create your own virtual connection between two machines on top of UDP. How to detect connection, filter out packets from other machines and detect disconnection via timeout.

Reliability and Flow Control

Learn how to implement your own reliability system on top of UDP using sequence numbers and acks. Also presented is a simple flow control system.

Debugging Multiplayer Games

Explains the techniques used by professional game developers to debug multiplayer games.

What Every Programmer Needs To Know About Game Networking

A brief overview of the history of PC multiplayer games. Discover how RTS games were able to synchronize thousands of units over a 28k modem, and how first person shooters hide lag with client side prediction and latency compensation.

Floating Point Determinism

Is it possible to get exactly the same floating point result (down to the bit-level) each time a program is run? How about across debug and release builds? Across different compilers? Different CPU architectures?

{ 62 comments… read them below or add one }

tuan kuranes October 15, 2008 at 7:21 am

Great series already !

Much Information I never found anywhere else.
(ie: tcp influence on udp when using both)

I’m sure next series will answer much other question I have (netgame loop, server + client on same host, network queue management with priorities, threading, compression benefits or not, …)

Here’s VS2005 working solution/proj and all (had to fix some compiler quirks, macro missing parenthesis, variable array allocation on stack, int to bool didn’t work in initialize connect, all working now.)

http://tuan.kuranes.free.fr/sendrecpackets.zip
http://tuan.kuranes.free.fr/VirtualConnectionUdp.zip

You could setup a code.google.com repository (svn +° wiki) with all you code so that patches and all becomes easier… meaning old articles code would also benefits from patches/updates.

Again, thanks for all the work !

Reply

Glenn Fiedler October 15, 2008 at 9:39 am

hey tuan, thanks for your help – there is a google code repository up here: http://code.google.com/p/netgame/

if you would like direct access then we can work together on it, just send an email to gaffer@gaffer.org and i’ll hook you up

thanks

Reply

Andrew October 21, 2008 at 9:58 am

Good job very interesting (Book marked) keep up the good work Glenn!

Reply

michael October 21, 2008 at 10:20 am

I really like your articles, i cant wait to read the new ones!.

Reply

emielongamedev October 24, 2008 at 5:07 am

A very interesting set of articles: the things you handle are difficult, but you managed to grab these things and make them readable. The new article is really good and I recommend everyone, who wants to know something about networking, reading that one. It’s easy to grab, but a powerfull concept.
Glenn, astonishing!

Reply

BB November 7, 2008 at 5:32 pm

HEY GLENN GET TO WORK GOD DARNIT! I’ve been waiting for the next articles for a long time.

Reply

Glenn Fiedler November 7, 2008 at 9:15 pm

sorry, i just started work at sony santa monica, so i’ve been a bit busy the last week

expect more articles around christmas, of course if sony approves them

cheers

Reply

BB November 9, 2008 at 6:11 pm

Hey thanks for letting us know. I have a question, what would you recommend to someone wanting to get up to speed in networking for games ?. I mean there’s enough info about the basics of networking but some of the info that you show here (at least have listed) is not easy to find in the web and you must have gathered all this from somewhere.

Btw do you know how to build networking systems for massive multiplayer games ? I’m not talking about super complex tech, but good enough to have, say 500 people in a single server. If you do then I’m sure millions of ppl would like to know how everything works and I would hate to have to stalk you day and night until you write such articles.

Seriously if you ever finish all the articles you listed here, a few extra on network architecture for mmos wouldn’t hurt.

Reply

BB November 9, 2008 at 6:20 pm

Or write a book.

Reply

Glenn Fiedler November 9, 2008 at 9:06 pm

ok well most of what i found i had to work out myself, but there are some good resources out there, check out the networking forum FAQ on gamedev.net thats a pretty good collection of articles

cheers

Reply

Scott November 14, 2008 at 9:09 am

Good articles! Though I’m really familiar with basic networking concepts, your articles are very well written and informative.

I am very much looking forward to your more indepth articles, especially on a node mesh and network synch.

~ Scott

Reply

SirKline November 17, 2008 at 3:45 am

Thank you for these articles. I have to fiddle with networking basics right now and these articles where the most helpful i’ve found. I hope to see more in near future.

Reply

Wizecoder November 25, 2008 at 7:16 pm

Do you have any information on the hardware side of things? Such as good server hosting plans, installing your server app with that hosting, etc…

Reply

HJ January 6, 2009 at 2:24 pm

Heya, I have recently taken a tumble down the nasty physics networking rabbit-hole. Your articles have been a great help with making sense of it all :)

I was wondering what your thoughts were on
. networking ragdoll physics (assuming [at a minumum] ragdoll root positions are important for gameplay)
. networking physics joints (in general)

Thanks for all the great articles
HJ

Reply

Glenn Fiedler January 6, 2009 at 6:38 pm

thanks, i just wish i had more time to finish the series! maybe after GDC

cheers

Reply

GR February 23, 2009 at 9:22 am

Glenn,

Thank you for your work on tutorials… I have looked at the code and first thing that got into my eye was that you are linking with wsock32.lib. If you look at MS documentation and chking the following link:
http://blogs.msdn.com/wndp/archive/2006/12/18/winsock-header-weirdness.aspx
you should notice that you should be using ws2_32.lib if you are including header winsock2.h to make sure nothing bad happens.

Reply

Glenn Fiedler February 23, 2009 at 4:53 pm

thanks i’ll update the code to match, cheers

Reply

SpeedRun April 1, 2009 at 11:24 pm

Great series of articles… hope to see more soon
posted a link on my blog

Reply

Jim Hubbard April 5, 2009 at 2:39 am

Is “Real World Networking” gonna happen anytime soon?

The stuff that I have read is really great! But, I really need to know how to actually connect clients through firewalls and NATs in the real world.

I’d make a donation for that info….

Reply

Glenn Fiedler April 5, 2009 at 11:24 am

Let me save you from waiting!

My recommendation in that article will be either A) use the XBox-Live, or PSN APIs for consoles (which handle NAT punch-through for you) or B) Use a middleware solution for the NAT punch-through on PCs.

You just don’t want to be writing this code yourself. It is too error-prone. Not even the pros roll their own here. Too many combinations of routers and weird behavior. Get somebody else to do it for you!

ps. No donation required for this info :)

Reply

Jim Hubbard April 5, 2009 at 11:43 am

Many thanks!

Are there any middleware services (for PC app development) that you would recommend?

Reply

Glenn Fiedler April 5, 2009 at 12:25 pm

take a look at RakNet and OpenTNL, cheers

Reply

Dan June 17, 2009 at 8:21 pm

Very interesting and valuable read. Do you have plans to finish the tutorials listed after ‘Reliability and Flow Control’ ? I would very much enjoy reading them. :)

Thank you for the work you’ve put in so far, it’s brilliant.

Reply

Glenn Fiedler June 17, 2009 at 10:29 pm

thanks!

i do plan to finish the articles, but I have no ETA right now i’m pretty busy at work!

Reply

Anonymous October 10, 2009 at 3:16 pm

Any update on the articles?

I’ve been wanting to try setting up an engine using event sourcing to get the state of an entity. But I’m not sure if there is too much overhead on calculating the state based on past events.

Reply

Glenn Fiedler October 14, 2009 at 9:01 pm

pretty busy working on my montreal talk coming up in a month. i hope to write the article on packet serialization shortly, also the slides from the talk should give you quite a bit of information – they will cover what you are interested in, basics of state based synchronization

cheers

Reply

William Fagerstrom October 27, 2009 at 5:52 am

Brilliant, your articles are truly great. I just finished a unit about network programming at Uni. and it wasn’t nearly informative as your articles. Though more broad but beside the point. I wish I had read this article before the socket assignment.

Reply

Glenn Fiedler October 27, 2009 at 12:26 pm

Thanks! Glad you like them!

Reply

hustnn December 7, 2009 at 7:10 pm

I am looking forward to your updates。

where can I find related resources such as related source code about your article

Reply

Glenn Fiedler December 8, 2009 at 8:17 am
hustnn December 8, 2009 at 9:07 am

Thanks.

Reply

Tomas Herman December 8, 2009 at 6:21 am

I really hope this series is not dead just yet. Its a really interesting reading which is quite easy to understand.

Reply

Glenn Fiedler December 8, 2009 at 8:17 am

Not dead, just sleeping – I’m really quite busy making games at Sony right now

Reply

Tomas Herman December 8, 2009 at 9:40 am

Thats good enough for me ;]

Reply

Glenn Fiedler January 29, 2010 at 11:36 am

Hey guys, some more articles have been added.

For the immediate future I plan to expand the “what every programmer needs to know about game networking” series then create a new series about networked physics to support my GDC2009 physics tutorial talk

After GDC I hope to return to the lower level networking articles, eg. reading/writing packets, sending events, synchronizing state etc…

cheers all

Reply

Glenn Fiedler January 29, 2010 at 11:38 am

Also, thanks to the folks who donated over christmas — keep ‘em coming! cheers all

Reply

varun vikram singh February 5, 2010 at 3:29 am

hi a m an aspirant of game programming
i m presently pursuing a graduation in computer science
and preparing for SAT and GATE
i need further consultancy in this field
i want to know that in which subject or sub field of game programming or development
i will do more study that is
will i study –
NETWORKING
COMPUTER GRAPHICS
ARTIFICIAL INTELLIGENCE
PHYSICS ENGINE
GAME NETWORKING ENGINE

i want to know which subject will i study hardest and most deeply
so that i will get more advantage in terms of experience , money , marketing and easiness
help me

Reply

Glenn Fiedler February 5, 2010 at 11:57 am

Study them all!

Reply

Evan Rogers February 13, 2010 at 11:08 pm

Your articles are great, Glenn. Keep it up :)

Reply

Glenn Fiedler February 14, 2010 at 10:16 am

Thanks Evan!

Reply

Arush Khan March 31, 2010 at 3:39 am

I love your articles Glenn you are very knowledgeable and I like the way you researched the history of network gaming and brought the reader from past to present.

Please keep writing!

When are you going to put a book together?

I am serious! :)

Arush

Reply

Glenn Fiedler March 31, 2010 at 3:11 pm

Thanks very much! Yes, eventually I’m sure I’ll write a book about networking. Hopefully it’ll be “THE” book about networking. We’ll see!

Reply

Vincent March 31, 2010 at 10:11 am

Hi Glenn,

Thanks so much for your articles :p
I’ve read this one (http://gafferongames.com/game-physics/networked-physics/) and I wonder if you can tell us more about server side distributed physics, do you think it could be interesting for MMO Game ?
How to distribute (on more than one servers) and compute the physics of a huge stacks of boxes for MMO game user ? Specialy MMOFPS game user, in fact :)

Vincent

Reply

Glenn Fiedler March 31, 2010 at 3:27 pm
Vincent April 1, 2010 at 4:23 am

Awesome presentation and demo !!!
Will it works in a MMO Game ? a MMOFPS game ?

Vincent

Reply

Glenn Fiedler April 1, 2010 at 9:59 am

It would need to be adapted to work with a client/server architecture but sure. You could just run the physics purely on the clients then have the server validate the physics and check it is within some tolerance. The bottleneck would of course be CPU cost on the server. Just FYI this is the hard thing I don’t believe there is any way around it =)

Reply

Glenn Fiedler April 1, 2010 at 10:00 am

Take a look at the “Sync Host” paper from Insomniac R&D to show how to use an authority scheme with dedicated server – cheers

Reply

Vincent April 2, 2010 at 2:06 am

I’m in need of a distributed physics engine for MMO. A solution to distribute a bunch of cubes like in your demo, but server side.

This is for MMO, therefore users have to be able to connect to the system, see and act is the virtual world…

Do you think it’s even possible ?

Reply

R Kline April 2, 2010 at 10:54 pm

Count me in among the “you should write a book” crowd. I’d race to buy it. The sort of information you lay out in your articles is really hard to find all-in-one-place. I learned more about the networking aspects of game programming from reading your article series than I have from months of on and off google searching. You have a real talent for presenting the info at a good pace, not too dumbed-down, but also not too overly detailed. PLEASE publish a book someday. You’d be doing the world a big favor. Also, this sort of info is good for sitting down on the couch and reading through, rather than loading into the web browser (though that’s alright too). Dead trees still have their uses.

Keep up the fantastic work.

Reply

Glenn Fiedler April 4, 2010 at 8:58 am

Thank you very much. I think my strategy would be to just keep writing articles on this site. Eventually, I’ll have critical mass to assemble these into a book. I’d rather not sit down and decide “today I’m writing a book!”. But it’ll probably happen eventually. cheers

Reply

Mark April 6, 2010 at 12:44 pm

Well, you could do it in a “Gems” sort of book. Make it a collection of useful articles that would make good reference material.

Reply

Martin April 3, 2011 at 5:06 pm

Thanks a lot for your efforts, I like your writing style and how you can make readers to understand complicated things easily.
I am doing some business apps for living, it is so boooring; so I am experimenting with various game programming related things to have fun and keep my brain in a good shape. Your articles really help keeping the coding fun and creative, and the best thing is that after reading each of your articles I can say – “oh my God, now I know how that complicated X stuff works and how to use it!”

Thanks again.

Reply

Glenn Fiedler April 3, 2011 at 7:09 pm

Thank you very much. I’m glad you enjoy them!

I find that many things which seemed complicated are usually easy once somebody explains the trick. You know that little non-obvious thing that shows exactly how things are done. Then it’s easy! For example, have you ever wonder how Little Big Planet has very stable physics collisions, with no tunneling and no jitter? It must be really complicated right?

Nope!

http://www.wildbunny.co.uk/blog/2011/03/25/speculative-contacts-an-continuous-collision-engine-approach-part-1/

:)

cheers

Reply

Dustin Dettmer April 8, 2011 at 10:56 pm

Have you considered putting this on the Kindle store? I hear it’s easy to do and it would be a lot nicer for me to read it there. Would easily pay $9.99 for the connivence.

Thanks!

Reply

Glenn Fiedler April 11, 2011 at 1:57 am

A great idea. I’ll look into it!

Reply

Joe Best July 22, 2013 at 8:58 pm

Hi Glenn,

I just watched your 2009 talk on your coop networking model, awesome stuff! Really got me thinking about a coop procedurally generated minecraftian open world game I’m working on. You mentioned at the end that you thought it needed ‘hardening’, has this been presented in more talks or articles since then? Also a guy came up to you and started explaining issues he’d had with a similar networking model, but the video cuts off before he explains. Would you still advocate the model from that talk? I was otherwise going to do client-server but trust the clients a lot to hide lag, but peer to peer would allow me to share AI processing cost, and means the server doesn’t need to deal with the overhead of calculating how relevant objects are for each client, maintain their network scopes, sort them constantly based on priority etc.

Reply

Glenn Fiedler July 24, 2013 at 6:59 am

The idea of distributing the cost is still sound however these days I’d do it in a client/server topology where the server is like a database with no sim. This makes late join and host migration easy. With p2p it’s virtually impossible.

Reply

imasha July 25, 2013 at 10:09 am

very well written, it was very helpful

Reply

CoolHandLuke August 1, 2013 at 3:46 am

Hi Glenn Fiedler,
I just recently discovered your articles on networking. I have to say it is by far best I have seen. Thank you for all the time you put into this.

Reply

Glenn Fiedler August 3, 2013 at 4:49 am

Thank you very much. I hope to write a few more articles soon

Reply

Boris Vasilyev January 5, 2014 at 4:43 pm

Hello Glenn, and thanks for your great articles! I really enjoyed them and would like to translate them to russian – so that more people could enjoy them:) Do you mind if I do this? I’ll keep all the links to the original, of course.
Thanks!

Reply

Glenn Fiedler January 6, 2014 at 5:56 am

No problem please feel free. And thanks!

Reply

Leave a Comment

{ 27 trackbacks }