A very common question that arises when designing a network application is whether to use TCP (Transmission Control Protocol) or UDP (User Datagram Protocol). There are a few guidelines and features that can determine which would be the preferred protocol.
TCP is a point to point stream based protocol. It is used in applications where reliability and data sequencing is needed: acknowledgments, error detection, retransmission of data, and will guarantee the data received will be sequenced in the same order as it was sent.
UDP is a connectionless protocol that does not guarantee delivery or data packet sequence (although each segment is numbered). It is a “send and forget” protocol that does not use acknowledgments. A common comparison is that TCP is similar to a phone call and UDP is similar to a post card (although the time difference between a phone call and the mail must be ignored). With TCP, you connect to a specific destination phone number. When that person answers they say “hello”, you say “Hi, my name is Bob”, and then the conversation continues with each side speaking and responding (in a well-behaved conversation!). With UDP you essentially transmit a datagram, like writing on a post card, and send it without verifying it was received or lost along the way.
When choosing between TCP and UDP, some major concerns are the overhead it takes to establish a TCP connection, speed and the reliability of data transmission. For example, SNMP uses UDP. SNMP is used to monitor networks, and many messages are sent and received for status updates and alerts. If TCP were used, the overhead of establishing, maintaining and closing a connection for each message and each host would create a lot of unnecessary traffic. A second example of when UDP is a better choice is when an application handles its own reliability at the application layer. Using TCP in this instance would be redundant.
As mentioned previously, TCP is a stream based protocol and UDP is a datagram based protocol. Let’s take an example of an application in which a NetBurner device takes A/D readings and sends them to another network device or host PC. Using UDP, each output operation (i.e. creating and sending a UDP packet) results in exactly one IP packet being created and sent. The result of taking and sending 10 A/D readings is that the host will receive 10 individual packets, each containing one reading. The host PC can then easily identify each reading, although each reading will have to be sent with a sequence number so that the reading order can be recreated. If TCP is used with a single continuous network connection (i.e. the connection is not closed and reestablished for each reading), you do not have control over how many readings are send with each IP packet. You would need to add start message and stop message identifiers to separate the data from each reading.
Some applications that use TCP are: HTTP, FTP, Telnet and SMTP. Some applications that use UDP are: DHCP, BOOTP, SNMP and DNS.