4 / 6 · Basic
TCP, UDP and ports
A port number is 16 bits, so it runs from 0 to 65535. Web servers using HTTPS listen on port 443.
Jump to the 10 practice questionsYou will learn
- Choose between TCP and UDP for a job
- Use ports to find the right program
Before this: How packets find their way
1Explain
Two puzzles
Two puzzles first. In a video call, one small piece of your friend's voice goes missing on the way. Should your app ask for it again? Now you are downloading a homework file, and one piece of the file goes missing. Same question. Decide both before reading on.
2Explain
Two missing pieces, two different answers
If you said no for the call and yes for the file, you have already chosen between the two transports. A piece of sound that arrives half a second late is useless. The conversation has moved on, so it is better to skip that piece and play the next one. A file with one piece missing is broken, so that piece is worth waiting for. The internet has a transport for each case: TCP waits and resends, UDP sends and moves on. Before either can help, though, a packet has to reach the right program.
3Problem
Why calls use UDP
Why do video calls often use UDP?
Hint
Sound that arrives late is no use.
Solution
Answer: Sound that arrives late is useless, so waiting for a resend does not help
For live audio, skipping a lost piece is better than pausing for it.
4Explore
Which program gets the packet: ports
An IP address gets a packet to the right computer. But one computer runs many programs at once: a browser with several tabs, a chat app, a music app. Both TCP and UDP headers carry a source port and a destination port, each a 16-bit number from 0 to 65535, and the port says which program a packet is for. A server program listens on an agreed port: 80 for plain HTTP, 443 for HTTPS, 53 for DNS, 25 for mail between servers. RFC 6335 splits the range into system ports 0 to 1023, user ports 1024 to 49151 and dynamic ports 49152 to 65535.
RFC 6335 splits the ports into three ranges: system 0 to 1023, user 1024 to 49151, dynamic 49152 to 65535.
const port = 443;
if (port <= 1023) console.log("system");
else if (port <= 49151) console.log("user");
else console.log("dynamic");system5Problem
The largest port
What is the largest port number?
Hint
A 16-bit number.
Solution
Answer: 65535
A 16-bit port has 2¹⁶ patterns including zero: 2¹⁶ − 1 = 65535. The largest port number is not the same as the total number of port numbers.
6Problem
How many ports
How many different port numbers are there?
Hint
Count 0 too.
Solution
Answer: 65536
Include zero when counting: 65535 − 0 + 1 = 65536 = 2¹⁶. The largest label is not the number of available labels.
7Problem
HTTPS
Which port does a web server use for HTTPS?
Hint
The port in the paragraph after HTTPS.
Solution
Answer: 443
443 is the standard port for HTTPS; 80 is plain HTTP.
8Problem
DNS
Which port does DNS use?
Hint
The same port is named in the DNS unit.
Solution
Answer: 53
DNS servers listen on port 53, over UDP and TCP.
9Problem
Port 50000
Your browser uses source port 50000. Which RFC 6335 range is that?
Hint
Above 49151.
Solution
Answer: Dynamic (49152 to 65535)
50000 is above 49151, so it falls in the dynamic range used for temporary ports.
10Explain
TCP checks off every byte
TCP (RFC 9293) turns an unreliable stream of packets into something like a phone line for bytes. First the two sides set up a connection with a three-way handshake: the client sends SYN, the server answers SYN-ACK, and the client replies ACK. From then on every byte has a sequence number. The receiver sends acknowledgements (ACKs) saying how far it has got, and puts out-of-order pieces back in place. If the sender hears no ACK within a timeout, it sends the data again.
11Problem
The handshake
Put the TCP handshake in order.
- Server sends SYN-ACK
- Client sends ACK
- Client sends SYN
Hint
SYN, then SYN-ACK, then ACK.
Solution
Answer:
- Client sends SYN
- Server sends SYN-ACK
- Client sends ACK
SYN asks to connect, SYN-ACK agrees and asks back, ACK confirms.
12Problem
The first timeout
Before any round trip has been measured, what first retransmission timeout does RFC 6298 set?
second
Hint
RFC 6298 starts at one second.
Solution
Answer: 1 second
RFC 6298 says to set RTO to 1 second until an RTT measurement exists.
13Explore
UDP sends and moves on
UDP (RFC 768, 1980) does much less. Its header is only 8 bytes: two ports, a length and a checksum. There is no handshake, no sequence number and no resending. For the video call in the first puzzle, that is exactly right. Short DNS questions use UDP too, because one question and one answer are quicker without a handshake. QUIC, the transport under HTTP/3, runs on UDP and builds its own resending on top.
Drag to turn. Scroll or pinch to zoom.
14Problem
TCP and a lost packet
With 25% loss, what does TCP do about a missing packet?
Hint
The purple packets in the simulator.
Solution
Answer: Sends it again after the timeout
Only the missing piece is resent; the receiver already has the rest.
15Problem
The UDP header
How many bytes long is a UDP header?
bytes
Hint
Two ports, a length and a checksum, 2 bytes each.
Solution
Answer: 8 bytes
RFC 768 assigns 2 bytes each to source port, destination port, length and checksum. The four fields total 4 × 2 = 8 bytes, not the size of the whole packet.
16Explain
Four numbers keep two tabs apart
Four numbers together identify one TCP connection: source address, source port, destination address and destination port. That is how a server on port 443 can talk to thousands of browsers at once without mixing them up. It is also how your laptop keeps two tabs on the same site apart, because each tab's connection uses a different source port. Tap a packet in the simulator and read its header. Before the home router the source is 192.168.0.12 port 51000.
| TCP | UDP | |
|---|---|---|
| Set-up | Three-way handshake | None |
| Lost data | Resent after a timeout | Stays lost |
| Order | Restored by sequence numbers | Not guaranteed |
| Header | 20 bytes or more | 8 bytes |
| Typical uses | Web pages, email, file downloads | DNS questions, voice and video calls, games, QUIC |
17Practice
Which pieces to resend
The receiver got the packets in the list. Finish missing(got, total) so that it returns the sequence numbers from 1 to total that never arrived, joined with spaces, or "none".
3 6
1 4
nonefunction missing(got, total) {
const lost = [];
return lost.length ? lost.join(" ") : "none";
}
console.log(missing([1, 2, 4, 5, 7], 7));
console.log(missing([2, 3], 4));
console.log(missing([1, 2, 3], 3));
Hint
Loop from 1 up to and including total.
A solution
function missing(got, total) {
const lost = [];
for (let n = 1; n <= total; n++) {
if (!got.includes(n)) lost.push(n);
}
return lost.length ? lost.join(" ") : "none";
}
console.log(missing([1, 2, 4, 5, 7], 7));
console.log(missing([2, 3], 4));
console.log(missing([1, 2, 3], 3));18Explain
Common mistakes
"UDP is a broken version of TCP that loses data."
UDP is chosen on purpose when late data is useless or one question and one answer are enough, as in calls, games and DNS. QUIC even builds its own reliability on top of it.
"A port is a socket on the back of the computer."
The word is shared, but a TCP or UDP port is a 16-bit number in the packet header, from 0 to 65535. It names a program, not a plug.
"When one packet is lost, TCP sends the whole file again."
Only the missing data is sent again after the timeout. The sequence numbers and ACKs show which part has not been acknowledged; the receiver already has the rest.
19
Summary
- Ports (16 bits, 0 to 65535) pick the program: 80 HTTP, 443 HTTPS, 53 DNS.
- TCP: handshake (SYN, SYN-ACK, ACK), sequence numbers, ACKs, resend after timeout.
- UDP: 8-byte header, no handshake, no resend; good when late data is useless.
- QUIC (HTTP/3) runs over UDP and adds its own reliability.