Sub topic 3
TCP versus UDP
This chapter assumes you know what an IP address is and that data travels across a network in packets. It also assumes you have seen the OSI and TCP/IP models from the previous chapter. You should be able to open a Windows command prompt. Here you will learn how the two main transport protocols differ, when each one is used, and which common errors point to a transport problem.
Where TCP and UDP fit
IP gets a packet from one host to another, but it makes no promises. Packets can arrive late, out of order, duplicated, or not at all. The transport layer (Layer 4 in the OSI model) sits on top of IP and decides how much help the application gets.
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the two transport protocols you must know for A+ Core 1. Both use port numbers to deliver data to the correct application on a host. The next chapter covers ports in detail. This chapter covers what happens to the data once it is addressed.
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented. A session is set up before data moves. | Connectionless. Data is sent with no setup. |
| Reliability | Acknowledges data and resends lost segments. | No acknowledgments and no resends. |
| Ordering | Numbers each segment and reorders them at the receiver. | No ordering. Datagrams may arrive in any order. |
| Flow control | Receiver tells the sender how much data it can accept. | None. |
| Overhead | Higher. Header is at least 20 bytes. | Lower. Header is 8 bytes. |
| Speed | Slower because of the extra work. | Faster and lightweight. |
| Data unit | Segment | Datagram |
How TCP works
TCP is described as reliable. That means it detects loss and repairs it. It does not mean the network itself is reliable. If the cable is unplugged, TCP cannot deliver anything. It can only report the failure after it gives up.
The three-way handshake
Before any application data moves, TCP opens a session with a three-step exchange.
- The client sends a SYN (synchronize) segment.
- The server answers with SYN-ACK, which acknowledges the request and offers its own synchronization.
- The client sends an ACK (acknowledgment). The session is now established.
When the conversation ends, each side sends a FIN (finish) segment and the other side acknowledges it. A RST (reset) segment ends a session abruptly. A client usually receives RST when it tries to reach a port where nothing is listening.
Sequencing, acknowledgment and retransmission
TCP gives every segment a sequence number. The receiver uses these numbers to put segments back in order and to notice gaps. It sends acknowledgments back to the sender. If the sender does not see an acknowledgment within a timeout, it retransmits the segment.
TCP also uses a window. The window is the amount of data a sender may transmit before it must wait for an acknowledgment. The receiver can shrink the window when it is overloaded. This is flow control. It stops a fast sender from flooding a slow receiver.
How UDP works
UDP adds only the port numbers, a length and a checksum to the data. It does not set up a session, so there is no handshake. It does not track what was received, so lost datagrams are simply gone.
This is a design choice, not a defect. Less work means lower delay and less bandwidth used. If a delivery guarantee matters, the application can add its own checks on top of UDP.
Choosing between them
Use TCP when every byte must arrive intact and in order. Use UDP when speed matters more than perfection, or when a single small question and answer is enough.
| Use case | Protocol | Reason |
|---|---|---|
| Web pages (HTTP, HTTPS) | TCP | A missing piece would corrupt the page. |
| Email (SMTP, POP3, IMAP) | TCP | Messages must arrive complete. |
| File transfer (FTP, SFTP, SMB) | TCP | Files must be exact. |
| Remote login (SSH, Telnet) | TCP | Commands and output must stay in order. |
| Voice and video calls | UDP | A late packet is useless. A short glitch is better than a pause. |
| Online games and live streams | UDP | Current data matters more than old data. |
| DHCP (67/68), TFTP (69), SNMP (161/162) | UDP | Short exchanges with low overhead. |
| DNS (53) | Mostly UDP | One small question and one small answer. |
Some services use both. DNS normally uses UDP for ordinary lookups. It switches to TCP when a response is too large for one datagram, and for zone transfers between DNS servers. RDP (3389) and LDAP (389) also appear with both protocols in the port reference table.
Seeing TCP and UDP on a real computer
The netstat command lists active connections and listening ports. The -a option shows all, and -n shows numbers instead of names. Run it on a Windows computer.
netstat -anTypical output looks like this.
Proto Local Address Foreign Address State
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 192.168.1.20:50712 93.184.216.34:443 ESTABLISHED
TCP 192.168.1.20:50730 93.184.216.34:443 TIME_WAIT
UDP 0.0.0.0:5353 *:*Read it like this:
LISTENINGmeans a TCP service is waiting for connections.ESTABLISHEDmeans the handshake finished and a session is active. Here a browser on local port 50712 talks to a web server on port 443.TIME_WAITmeans the session recently closed and the system is waiting briefly before it releases the port.- UDP lines have no state, because UDP has no session. The foreign address is shown as
*:*.
The local port 50712 is an ephemeral port. The client picks it at random from a high range for the length of the session. The server side uses the well-known port.
Common errors and what they mean
| Symptom | Likely transport-level cause |
|---|---|
| Connection refused or reset right away | The host is reachable, but nothing listens on that TCP port. The host replied with RST. |
| Connection times out | A firewall silently drops the SYN, or the host is down or unreachable. |
| Web page loads slowly or stalls | Packet loss forces TCP retransmissions. |
| Video call is choppy, but the web works | UDP traffic is being lost or delayed. TCP hides loss, so the web seems fine. |
| Ping works, but an application does not | ICMP is allowed, but the application's TCP or UDP port is blocked. |
| Service works on a LAN but not through the router | Port forwarding or firewall rules cover the wrong protocol. |
The last two rows matter in practice. When you create a port forwarding rule on a SOHO router, you choose TCP, UDP, or both. Choosing the wrong one makes the rule appear correct while traffic never passes. Also remember that ping uses ICMP, which is neither TCP nor UDP. A successful ping proves IP connectivity, not that a port is open.
Summary of the key points
- TCP is connection-oriented, reliable and ordered. It uses the three-way handshake (SYN, SYN-ACK, ACK), sequence numbers, acknowledgments and flow control.
- UDP is connectionless, best effort and lightweight. It is used where speed and low delay matter more than perfect delivery.
- Both protocols use port numbers, and some services such as DNS use both.
netstat -anshows TCP states, while UDP entries have no state.
Exercise
A technician tests a VoIP phone system. Calls connect, but the audio has short gaps whenever the network is busy. A colleague suggests switching the voice traffic to TCP so no packets are lost. Explain why this would probably make the call worse, and name the transport protocol VoIP audio normally uses.
Show answer
VoIP audio normally uses UDP. With TCP, a lost packet is retransmitted and later packets wait behind it, so the delay grows and the call lags. By the time the retransmitted audio arrives it is too old to play. UDP skips the lost packet, which causes only a brief gap, and the call stays in real time.
Exercise
You run netstat -an and see a line showing a TCP connection in the ESTABLISHED state to a remote address on port 443. Which handshake steps have completed, and what service is most likely in use?
Show answer
All three steps of the handshake (SYN, SYN-ACK, ACK) have completed, so the session is open. Port 443 is HTTPS, so this is most likely a web browser or application using a secure web connection.
Exercise
A user can ping a server by name, but a connection to its web service on TCP port 80 fails immediately with "connection refused". Does this point to a network cable fault, a firewall dropping packets, or a service problem on the server? Explain.
Show answer
It points to a service problem on the server. The ping success shows IP connectivity and name resolution work. A "refused" error means the server answered with a TCP reset because nothing is listening on port 80. A firewall that drops packets would usually cause a timeout instead of an immediate refusal.