Other Posts in Networking

  1. TCP/IP vs. UDP
  2. Pinging Another Computer Using C#

TCP/IP vs. UDP

2/9/2008

One of the many questions that seems to be asked all the time is "Which one is better UDP or TCP/IP?" And everyone runs to their preferred corners and touts one over the other. In fact this happens every time that anyone asks "Which one is better..." no matter what they're comparing. The truth of the matter is that it really depends on what you are making and the networking topology that your using as to which is better.

TCP/IP

TCP/IP is a great networking protocol and has many things going for it:

  • its reliable
  • you get the data in order of it being sent
  • its connection oriented

This basically means that most of the programming that you would need if you used UDP is done for you, making your job that much easier. For most games, this works perfectly fine but there are some problems. Namely if there is an error in the line you have to wait for TCP/IP to resend the data. In fact you can't send anything else because the data has to be received in order. Another problem, although you can turn it off, is Nagel's algorithm. This basically collects a bunch of smaller packets and then sends them all in one shot. If you're unaware of Nagel's algorithm's existence, this can lead to some disappointing ping times. Also since it is connection oriented its really only fit for client/server applications. While it can be used in a peer-to-peer topology, it can lead to some problems.

UDP

UDP is pretty much the polar opposite of TCP/IP and has certain problems for game development:

  • your data is not guaranteed to get to its destination
  • the packets may be received out of order
  • there is no connection

Now why would we want to use UDP? Simple, it's fast. Because UDP doesn't make any guarantees, all it does is send the packet. There's no overhead making sure it gets there or really anything. But that does mean that we have to deal with lost packets, out of order packets, etc. ourselves. This basically just means that we have to potentially rewrite TCP/IP's functionality, well sometimes anyway. And in cases where you need that reliability, it pretty much gets rid of the speed factor as both will be running in the same manner. However due to the way in which UDP works, it is generally useful for any game type that requires less reliability and more speed. So a turn based game or an RPG might not need to use UDP, but an FPS would definitely want to use it.

Topology Concerns

One thing to also keep in mind is the topology that you are using.  Since TCP/IP is connection based, it is ideal for client/server connections. UDP on the other hand is more apt for peer to peer connections for a couple of reasons, but one of the main reasons is NAT punch through. "What the heck is that?" I'd say that in today's world most people are behind a router of some sort. On one side of the router you have an IP, on the outside though everything is sent using the router's IP. So if two people from behind a router connect to a game, how can we tell them apart? In fact, how can we connect to someone who is behind a router? Well the person can open up a port on the router to be sent to their computer or you can use an introducer. Basically an introducer is a server which is used to connect the clients so they can connect directly to one another. The way this is done, is by first connecting to the introducer. Once connected, the introducer sends the IP and port information of the clients to one another. This allows the clients to send data directly to one another using that information. This essentially allows for peer to peer connections across networks and is used by many peer to peer programs. If you want to know more you can read about it here.

Conclusion

So as you can see, neither of them is really better. Both have their place within game development and in fact sometimes both are used at the same time depending on the game. Basically this means that depending on the topology you plan to use, the type of game, and also whether reliability or speed is more important will decide which protocol is best.



Comments