UDP or TCP?

Started by
8 comments, last by Antheus 11 years, 12 months ago
I am working on a RTS or maybe a turn based strategy game, IIRC UDP is better for RTS, FPS games, so should I use UDP or stick with TCP?

has this changed at all in the last few years? or is UDP the way to go?

Thanks!
Advertisement
This depends entirely on your network model.

If you intend to do lockstep simulation, stick with TCP as guaranteed delivery is important and you don't want to have to reimplement that on top of UDP. If you're going for only-show-latest-data, UDP is fine since you can always update once new data comes in.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Given that Starcraft, Age of Empires, and Warcraft all use TCP, I don't understand why you think UDP is better for a RTS.

The main thing UDP has going for it is that NAT punch-through is easier to build. However, there are many other things you need to have before NAT punch-through is even important (much less possible,) so I wouldn't worry about that right now.
enum Bool { True, False, FileNotFound };
If you require confirmation of data retrieved, i.e. you are making a synchronous system, TCP is a must
if you just need the latest data, as fast as posible, UDP is a better option.

for a RTS/turn based, skill shots is usually less of a requirement than having all the data you need. Also, TCP always seems to be efficient enough for a reliable connection speed.
Is it reasonable to have a combination of both? I'm confused exactly what the broadcasting life of a fired bullet might look like.

Would you TCP message a server that a bullet was fired to make sure the hit happened? Would that be responsive enough? Or would you UDP update the bullet's position/velocity along like the player position/velocity over the bullet's lifetime and hope that one of the packets makes it? Is general latency such that a bullet's lifetime lasts less than a UDP trip to a server so the game might only get one packet off, which might be lost? In which case would you have to UDP along the bullet's position several times, possibly even after the bullet's lifetime, to make sure that one of the packets makes it? If so, how long should you send these packets after the original shot?

What's the general way to handle traffic for bullets or other short-lived events? and via UDP or TCP?

I might be network paranoid/mistrusting. o_O

Edit: Up there I was assuming the creation of the bullets happened on the client side. You might also UDP along that the player is holding down the fire button, by which the server will react whenever it catches on. It would then create the bullets and manage them server side, and UDP the position/velocity of the bullet back to the player. The client physics engine would take it over as if a regular particle, and its visual look would then have no implications on what happened. Is it still possible for some freak packet loss circumstance that makes it so the player's bullets aren't displayed, but still acted on? >_>
UDP is the wrong thing to use if you need to know what's going on, period.

Either you use TCP and get the job done, or optionally use a reliability layer on top of UDP (which is, IMHO, almost always a waste of time, except for really niche applications, since you could have just used TCP with appropriate configuration). Otherwise, something like this happens:

- Player hits Fire button for 1/5th of a second, which is, say, 6 frames.
- UDP packets are lost for 8 frames around this time, due to network hiccup.
- All the UDP packets saying "fire" are gone
- Other player, being on a different network, hits Fire at this time and his packets make it to the server
- The server then "reliably" tells the first player he is dead
- Player ragequits because he shot first and still died, and then tells everyone on reddit that your game is shitty

This kind of thing happens all the time in network models that are not designed for reliability. If you need to know when something happens, don't use UDP.

That's not to say TCP is a panacea or magic sauce, though. Look at early releases of Halo 2 for examples: they had a terrible latency compensation model, and players were constantly complaining that they pulled the trigger and then died 2-3 seconds later of mysterious circumstances.



TL;DR: networking is hard. Don't make it harder than it has to be by trying to overcomplicate things with unreliable network protocols.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Lastly for me, is it uncommon to see both TCP and UDP built into a server? TCP to get critical stuff done, and UDP to pump out ever-moving "acceptably lost" data?
Using TCP + UDP is sometimes done, but it kind-of has the worst performance of both. TCP simetimes likes to jam really big segments down the uplink, causing jitter in packet delivery of the UDP half. Also, TCP doesn't play as nice through NAT punch-through as UDP.
Thus, you either stick with TCP, and accept that sometimes data is delayed, or you go with UDP, and once you have UDP working, you might as well put a thin reliable/re-send layer on top of that UDP code.
enum Bool { True, False, FileNotFound };
TCP will throttle UDP badly if you get into traffic contention issues.

I would not mix and match. use one or the other.

If latency is not an issue and you can use a deterministic model a-la Starcraft 2, TCP is good to go.

Everything is better with Metal.

A lot of theory was crafted studying TCP vs. UDP, deterministic vs. non-deterministic...

And then there's this.

Flash, TCP, in browser, 85 players always updated, per-pixel hit tracking, works across continents, twitch that makes Diablo look like turn-based Chess match.

Networks are fine, so are computers. The rest will be on how things are handled on client and server.

This topic is closed to new replies.

Advertisement