edubun
English
1 / 19

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 questions

You 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.

Port
const port = 443;
if (port <= 1023) console.log("system");
else if (port <= 49151) console.log("user");
else console.log("dynamic");
Output
system

5Problem

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.

  1. Server sends SYN-ACK
  2. Client sends ACK
  3. Client sends SYN
Hint

SYN, then SYN-ACK, then ACK.

Solution

Answer:

  1. Client sends SYN
  2. Server sends SYN-ACK
  3. 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.

Loading

Drag to turn. Scroll or pinch to zoom.

Loss is set to 25%. With TCP, green ACKs come back for each packet; when one is missing, the laptop waits for the timeout and sends that packet again (purple), so the server always ends with the whole message. Switch to UDP and send again: lost packets leave dots in the message. In this model ACKs are never lost and every packet gets its own ACK; real TCP sends cumulative ACKs.

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.

TCPUDP
Set-upThree-way handshakeNone
Lost dataResent after a timeoutStays lost
OrderRestored by sequence numbersNot guaranteed
Header20 bytes or more8 bytes
Typical usesWeb pages, email, file downloadsDNS 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".

Expected output
3 6
1 4
none
Your code
function 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.

Next unitDNS: turning names into addresses
Full text · The whole lesson as one text

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.

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.

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. Your computer picks a temporary source port for each conversation, often from the dynamic range. An address and a port together, such as 198.51.100.20:443, name one end of a conversation exactly.

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. RFC 6298 sets the first timeout at 1 second and then adjusts it to the measured round-trip time. TCP also slows down when it sees loss, so one busy link is not flooded.

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.

Loss is set to 25%. With TCP, green ACKs come back for each packet; when one is missing, the laptop waits for the timeout and sends that packet again (purple), so the server always ends with the whole message. Switch to UDP and send again: lost packets leave dots in the message. In this model ACKs are never lost and every packet gets its own ACK; real TCP sends cumulative ACKs.

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. After it, NAT has changed both numbers, and the router remembers the swap so replies go back to the right tab.

TCPUDP
Set-upThree-way handshakeNone
Lost dataResent after a timeoutStays lost
OrderRestored by sequence numbersNot guaranteed
Header20 bytes or more8 bytes
Typical usesWeb pages, email, file downloadsDNS questions, voice and video calls, games, QUIC
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.
Teacher's noteAsk the two puzzle questions before you say TCP or UDP. Once learners have decided for themselves that a late piece of sound is worthless, the two names become labels for a choice they already made. The handshake sticks when three people act it out: one at each end, and one in the middle carrying cards marked SYN, SYN-ACK and ACK. Where they get stuck is the four numbers that name a connection. Write the connections of two tabs side by side and let them spot that only the source port differs.
Key points
  • 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.

Practice questions from TCP, UDP and ports on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (27)
  • (checked 2026-10-17)
  • IETF RFCs (rfc-editor.org): RFC 768 UDP (1980, 8-byte header)
  • RFC 791 IPv4 (1981, 32-bit addresses, "does not provide a reliable communication facility", TTL field "measured in units of seconds" but every module must decrease it by at least one, so it is only an upper bound)
  • RFC 792 ICMP (Time Exceeded)
  • RFC 801 NCP/TCP transition plan (1 January 1983)
  • RFC 894 Ethernet (1500-byte payload)
  • RFC 1034 and RFC 1035 DNS (1987, HOSTS.TXT kept by SRI-NIC, labels up to 63 octets and names up to 255, TTL in seconds)
  • RFC 1122 (127.0.0.0/8 loopback)
  • RFC 1918 private addresses (10/8, 172.16/12, 192.168/16)
  • RFC 2131 DHCP, RFC 3022 NAT, RFC 3596 AAAA records, RFC 3849 and RFC 5737 documentation addresses, RFC 4271 BGP-4, RFC 4291 IPv6 text form, RFC 6298 TCP retransmission timer (initial RTO 1 second)
  • RFC 6335 port ranges (0-1023, 1024-49151, 49152-65535)
  • RFC 7766 DNS over TCP, RFC 8200 IPv6 (128-bit addresses, Hop Limit)
  • RFC 8446 TLS 1.3 (one round-trip handshake)
  • RFC 9000 QUIC and RFC 9114 HTTP/3, RFC 9110 HTTP semantics (status code classes)
  • RFC 9293 TCP (three-way handshake, sequence numbers)
  • IANA: ip-parameters registry (recommended default TTL 64)
  • IPv4 free pool depleted 3 February 2011 (NRO and ARIN announcements)
  • Official documentation: MDN Web Docs "How does the Internet work?" (45 cables for 10 computers, switches, routers, MAC and IP addresses, internet and web)
  • "What is a URL?", "HTTP response status codes" (developer.mozilla.org)
  • Cloudflare Learning Center: "What is DNS?" (four kinds of DNS server, 8 steps of an uncached lookup, browser and OS caches, TTL)
  • "What is HTTP/3?" (cloudflare.com/learning)
  • Google Chrome Help, "Browse in Incognito mode" (support.google.com/chrome/answer/95464, checked 2026-10-19: Incognito limits what is saved to the device; websites and organisations that manage your network, like your school, employer or internet service provider, may be able to observe your activity)
  • Operators and papers: root-servers.org (13 root name servers run by 12 organisations, 2047 instances on 2026-10-17)
  • Jacobson and Karels 1988, Congestion Avoidance and Control (loss mostly congestion)
  • History blocks only: UCLA Samueli School of Engineering, Internet50 (first ARPANET message "LO", UCLA to SRI, 29 October 1969, 22:30)
  • historyofinformation.com and Wikipedia ARPANET (Kline)
  • Spiceworks/SRI history (HOSTS.TXT kept by SRI-NIC).

Checked: 2026-10-17