TCP: Reliable, Connection-Oriented
TCP is connection-oriented — before data transfer, a session is established using the three-way handshake: (1) SYN — client sends synchronize to server. (2) SYN-ACK — server acknowledges and sends its own synchronize. (3) ACK — client acknowledges. The session is now established. Termination uses a four-way FIN/ACK sequence.
TCP guarantees delivery by requiring acknowledgment (ACK) for every segment. If an ACK is not received within a timeout period, the sender retransmits. TCP provides: ordered delivery (sequence numbers ensure correct reassembly), error detection (checksum), flow control (window size — prevents overwhelming receiver), and congestion control (reduces transmission rate when network is congested).
TCP port numbers identify applications. Source port: a random high-numbered port chosen by the client for this session (ephemeral port, typically 1024–65535). Destination port: the well-known port of the service (80 for HTTP, 443 for HTTPS). The combination of source IP, source port, destination IP, and destination port (the four-tuple or socket pair) uniquely identifies each TCP connection.
UDP: Fast, Connectionless
UDP is connectionless — it sends data without establishing a session first. No handshake, no acknowledgment, no guaranteed delivery, no ordering. UDP is a 'fire and forget' protocol. This makes UDP significantly faster and lower latency than TCP — there is no round-trip handshake overhead before data starts flowing.
When to use UDP: real-time applications where speed matters more than guaranteed delivery — VoIP, video streaming, online gaming, DNS queries. A lost voice packet is better discarded than retransmitted (retransmitting old audio causes glitching). DNS queries are typically one UDP request and one UDP response — connection overhead would waste time. TFTP and DHCP also use UDP.
Applications can build their own reliability on top of UDP when needed. QUIC (used in HTTP/3) is a modern protocol that implements reliable, multiplexed transport over UDP, combining UDP's speed with TCP-like reliability and TLS encryption.
TCP vs UDP Decision Framework
Choose TCP when: data integrity is critical (file transfers, web pages, email, database queries), ordered delivery matters, or the application cannot tolerate missing data. Choose UDP when: low latency is critical (voice/video), some data loss is acceptable, the application implements its own error handling, or the transaction is a single request-response (DNS, DHCP, SNMP, TFTP).
Common exam question: 'Which protocol should be used for a VoIP application?' Answer: UDP — because even a small retransmission delay causes audible glitching, and a lost voice packet is simply skipped. Similarly: 'Which protocol ensures all data is received and in order?' Answer: TCP.