25

This is a question I see a lot. Most people say UDP is always better for real-time games than TCP. My understanding is that TCP tries to re-send packets over and over til the other side gets them whereas UDP doesn't care.

Most of the things I've read is that UDP is a must for any realtime game and TCP is terrible. But the thing is, most people seem to implement some form of TCP on top of UDP anyways. And I've also heard that the difference between the two is negligible given that we're not in the 80s anymore and the internet is now pretty fast and reliable.

Is my general understanding here wrong? Can someone clear this up for me?

zypA13510
  • 101

2 Answers2

13

Depends on if you're talking about peer-to-peer, client/server with the users running the server, or client/server with a data center running the server. Only in the latter-most case is the internet really fast and reliable. Your users' computers are not guaranteed to be fast, and certainly won't be reliable.

UDP allows you greater control over the sort of TCP-like implementation you're making. It gives you greater flexibility to execute packets out of order, discard packets that you consider unnecessary while retrying packets you consider important, that sort of thing. But this should only be done if needed and if you have the necessary expertise.

If you can do without that flexibility, TCP works well enough and saves you a whole lot of time. Even professional studios (like one I worked at) use TCP if they don't absolutely need UDP, and they have people dedicated to network programming.

Kevin Fee
  • 2,847
4

It would be an assumption to say "Internet is now pretty fast and reliable" as @Ordous pointed out, and a dangerous one too.

The reason why UDP+custom protocol for delivery-critical packets does magic on most games is that, there are times when it could be "okay" if you lose some packet (just for. e.g. secondary non-critical events to complete game play), there are also times where its "not at all okay" to lose some data for e.g cursor movement etc. (I don't do game development for a living so pardon my vague-ish examples)

UDP doesn't waste time in pushing them again and again, by default.

And, many games incidentally seem to have the "okay to lose sometimes" packets more than "always need to deliver without fail" packets. Hence making a natural fit for this task.

All that was needed on UDP was to use a custom protocol that just helps deliver the "always need to deliver without fail" packets properly, leaving the rest of game data to the mercy of the network connection.

Now deciding on what kind of traffic makes up most of YOUR data to be transmitted across will help you decide better.

The point against TCP would be that time spent on retries could rather be spent on sending packets that matter NOW.

There is also a chance that if there is any issue during transmission, TCP could cascade to a more broken down game-play scenario for the user, spoiling their experience compared to UDP+Custom Stack (This last part is just hunch. I will leave this to other experts here to comment on this. Would love to learn about possibilities of this scenario).

onkkno
  • 289