Game Networking

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?

94 thoughts on “Game Networking”

  1. 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 !

  2. Pingback: GameDevMike : Networking for Game Programmers
  3. 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!

  4. 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.

  5. 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

  6. 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

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

  8. 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…

  9. 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

  10. Pingback: Программирование многопользовательских игр « Валерий Степанов
  11. 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….

    1. 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 :)

  12. 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.

  13. 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.

    1. 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

  14. 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.

  15. I am looking forward to your updates。

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

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

  17. 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

  18. 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

  19. 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

  20. 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

    1. 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 =)

      1. 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 ?

  21. 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.

    1. 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

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

  22. 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.

  23. 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

  24. 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!

  25. Pingback: 终将游戏一生 » TCP vs UDP
  26. Pingback: Game Development Resources | Michel Rinck
  27. Pingback: Network Programmer Blogs (especially for games) | Question and answer
  28. Pingback: Client-server Application - Startup (PART 1) | Exasm
  29. Pingback: Networking for Game Programmers | Johnny Rocketpants
  30. 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.

    1. 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.

  31. 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.

  32. 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!

  33. Pingback: Сетевое программирование для разработчиков игр. Часть 1: UDP vs. TCP « Домик Миа
  34. Pingback: [Перевод] Сетевое программирование для разработчиков игр. Часть 3: виртуальные соединения поверх UDP | Вести3.ру — Информационный журнал
  35. Pingback: Сетевое программирование для разработчиков игр. Часть 2: прием и передача пакетов данных « Домик Миа
  36. Pingback: Сетевое программирование для разработчиков игр. Часть 3: виртуальные соединения поверх UDP « Домик Миа
  37. Pingback: [Перевод] Сетевое программирование для разработчиков игр. Часть 3: виртуальные соединения поверх UDP » CreativLabs
  38. Nice article about the matter. Although I am now still sitting in font of my PC not knowing how to implement everything I have learned here so far because there are still some question left. Like for example if the network code should run in its own thread (read somewhere it shouldn’t but with no further explanation) or how to implement this exactly in an object orientated environment.
    However I realized now that networking seems to be way more ressource critical then I ever imaginennd it would. Especially on ressource critical systems like smartphones this makes it even harder to implement something good.
    So far what I have learned is that I am going to use UDP hole punching for a ‘client to client’ connection. Also fighting against cheaters is like fighting against piracy… useless.

    1. Get it working on a single thread non-blocking first. If there are performance problems with that, move it to another thread as an optimization. Don’t feel you need to do it multithreaded initially.

      Next, for NAT punch through sure if you do client->client you would have to do that. So don’t :). Do client/server and have a server in a data center and no punching is required. This is the way ™. NAT punching and P2P are stupid and a waste of time.

Leave a Reply

Glenn Fiedler's Game Development Articles and Tutorials