Choosing Between TCP and UDP

Most of the time, it should be pretty easy to decide between UDP datagrams and TCP connections. If you must be sure that a message has been received or that it hasn't, you should use TCP. Also, if you need to know when a client or server terminates, use TCP. Because UDP datagram packets have a size limitation of roughly 1,100 bytes, you should use TCP connections if you need to send more than about 1,000 bytes of data.

If you need to send data to anyone that might be listening, especially to multiple receivers, a UDP datagram is probably a better idea—especially if it doesn't matter if the message is never received. UDP datagrams are also good for sending data quickly because there's less setup overhead. If you need ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.