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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access