[MUD-Dev] UDP vs TCP/IP

Andrew Kirmse akirmse at pacbell.net
Sun Jun 24 00:17:00 CEST 2001


I bet this topic comes up often... here are some things to keep in mind:

- TCP is an excellent bulk transfer protocol.  It is well-researched and
usually well-implemented.  Its main drawback for games is that it's in
order, so that one lost segment prevents any others from being delivered.
This can cause long "lag" periods during network congestion.  Although it's
quite possible to implement an entire MMORPG with TCP (Ultima Online and
Meridian 59, for example), in general it's possible to get better tolerance
to dropped packets with UDP, at the cost of a more complex implementation.

- UDP is well-suited to frequently updated, non-critical data, like object
positions.  You must be prepared for a high degree of packet loss for UDP,
however.  Some routers preferentially drop UDP during periods of high
network congestion.

- TCP does not give enough control or status information for a typical game.
For example, it's impossible to tell how many segments are being lost.
However, it's still well-suited for time-insensitive data that needs to be
reliable.  Examples would be chat, file transfers, login, etc.

- Although TCP's headers are theoretically larger than UDP's (20 bytes
versus 8), most ISP's support Van Jacobson compression for TCP, bringing its
header down to about 3 bytes on a serial link.  Thus, on a modem connection,
UDP is actually less space efficient.

- UDP packets may be any size (IP will fragment them if necessary), but
performance may suffer if the packets are larger than the smallest
non-fragmenting size (~576 bytes).  This is because the loss of any one
fragment will cause loss of the whole packet.  It's good to aim for this
size for UDP packets---any larger and you'll fragment, any smaller and
bandwidth is lost to header overhead.

- The TCP acknowledgment and resend mechanism is quite efficient, to the
point where it's probably more efficient than most custom schemes using UDP.

- When implementing a reliability layer on UDP, it is very easy to fall into
one of the classic performance problems that TCP has solved over the years.
Get to know TCP's solution intimately before writing your own, or better
yet, use TCP for reliable communication.

- In general, a TCP-based design will use more resources than one based on
UDP.  This is both because TCP has more per-endpoint state data than UDP,
and because a TCP server requires more sockets than a UDP server (one per
client for TCP, usually only one or two total for UDP).

- It is tempting and often wise to use both TCP and UDP in the same
application.  Keep in mind that this requires more configuration to play
behind a firewall.

- If I read it right, JC Lawrence implied that it's possible to write your
own protocol directly on IP.  However, all current versions of Windows lack
support for these so-called "raw sockets".  Windows XP and Unix have them.

- I highly recommend the book "TCP/IP Illustrated, Volume I" by W. Richard
Stevens for clear explanations of all things TCP/IP.

---Andrew

_______________________________________________
MUD-Dev mailing list
MUD-Dev at kanga.nu
https://www.kanga.nu/lists/listinfo/mud-dev



More information about the mud-dev-archive mailing list