Ports

A port number picks the program; five values name the conversation

Laptop10.0.0.25Web server203.0.113.10TCP 443listening51234browser tab 1TCP · 51234 → 44351235browser tab 2TCP · 51235 → 443TCP 44353001name lookupUDP · 53001 → 53UDP 53DNS resolver10.0.0.1A port number picks the program; source and destination address and port, with the protocol, name one conversation.Servers listen on fixed, well-known ports such as 443 for HTTPS. The client picks a temporary port for each connection, so twotabs talking to the same server stay apart. TCP and UDP have separate port spaces: TCP 53 and UDP 53 are different doors.The five values, protocol, source IP, source port, destination IP and destination port, are the 5-tuple a firewall tracks.

A port picks the program. Protocol, source and destination address and port form the 5-tuple that names each conversation.

Ports · Going deeper

Port ranges and the sockets on a machine

Well-known: 0–1,023Registered: 1,024–49,151Dynamic (IANA): 49,152–65,535Linux client ports by default: 32,768–60,999016,38432,76849,15265,535$ ss -tunNetid State  Local Address:Port    Peer Address:Porttcp   ESTAB  10.0.0.25:51234        203.0.113.10:443tcp   ESTAB  10.0.0.25:51235        203.0.113.10:443udp   ESTAB  10.0.0.25:53001        10.0.0.1:53Ports below 1,024 are reserved for well-known services; clients borrow a high port for each connection.IANA registers ports 1,024 to 49,151 for specific applications and leaves 49,152 to 65,535 for temporary use; Windows usesthat range and Linux uses 32,768 to 60,999. ss on Linux and netstat -an on Windows list every socket with both ends.

Well-known ports run 0–1,023; clients borrow ephemeral ports, 49,152–65,535 on Windows and 32,768–60,999 on Linux.

Handshake

TCP spends one round trip agreeing to talk

ClientServer :4430 ms40 ms80 msSYN · seq 0SYN-ACK · seq 0, ack 1ACK · ack 1, then the 420-byte requestfirst bytes of the reply40 ms round tripTCP spends one round trip agreeing to talk before the first byte of the request can be sent.The client sends SYN with its starting sequence number, the server answers SYN-ACK with its own, and the client acknowledges.Both sides now know where the other’s byte count starts. On a 40 ms path the request leaves after 40 ms and the replybegins at 80 ms; TLS 1.3 adds one more round trip before HTTPS can send anything. Real starting numbers are random.

The three-way handshake, SYN, SYN-ACK, ACK, costs one round trip: on a 40 ms path the request leaves at 40 ms.

Handshake · Going deeper

Closing a connection and the TCP states

ClientServerCLOSEDLISTENserver waitsSYN-SENTSYN-RECEIVEDhandshakeESTABLISHEDESTABLISHEDdata flowsFIN-WAITCLOSE-WAITone side doneTIME-WAITCLOSEDwait 2 × MSL✓FIN, then ACK, from each side: four messages to close✗RST: abort now, no goodbye (closed port, firewall, crash)flags  SYN  ACK  FIN  RST  PSH  URGbits   1    1    1    1    1    1Each side closes its own direction with FIN; RST tears the connection down at once.The side that closes first waits in TIME-WAIT, 60 seconds on Linux, so late packets of the old connection die out.

Each side closes with FIN; RST aborts. The closer waits in TIME-WAIT so stray packets die out.

Reliability

TCP numbers every byte, so it resends exactly what was lost

ServerClientseq 1 · 1,460 bytesACK 1,461next byte I want✗ lostseq 1,461 · 1,460 bytesseq 2,921 · 1,460 bytesACK 1,461duplicate 1seq 4,381 · 1,460 bytesACK 1,461duplicate 2seq 5,841 · 1,460 bytesACK 1,461duplicate 3: resend nowseq 1,461 againfast retransmitACK 7,301everything up to hereTCP numbers every byte, so the receiver can say exactly what is missing and the sender resends only that.The acknowledgement is the next byte the receiver expects. Segment 2 is lost, so every later arrival repeats ACK 1,461.Three duplicates tell the sender to resend at once, about one round trip after the loss, instead of waiting for its timer(at least 200 ms on Linux, 1 s in the RFC). The single ACK 7,301 then confirms all 5 segments, 7,300 bytes.

The acknowledgement number is the next byte expected. Three duplicate ACKs trigger a fast retransmit of sequence 1,461.

Reliability · Going deeper · 1 of 3

Segment size and the options agreed at the start

Eth 14IP 20TCP 20data: MSS 1,460 bytesFCS 4One full segment on Ethernet: the IP packet is the 1,500-byte MTU, so TCP carries 1,500 − 20 − 20 = 1,460 bytes of dataFeatureWhat it doesAgreed inMSSlargest data per segment, 1,460 on EthernetSYN and SYN-ACK optionsSACKreceiver lists blocks it holds, so only gaps are resentSYN options (RFC 2018)Window scalemultiplies the 16-bit window by up to 2¹⁴SYN options (RFC 7323)Timestampsmeasure round trip time on every segmentSYN optionsTCP cuts the byte stream into segments that fit one packet; the options agreed in the handshake tune the rest.A VPN or tunnel adds its own header, so the MSS must shrink or packets are fragmented or dropped: on Cisco routers,ip tcp adjust-mss 1360 rewrites the MSS in passing SYNs. Selective acknowledgement lets one ACK report several gaps.

The MSS is 1,460 bytes on Ethernet: the 1,500-byte MTU minus IP and TCP headers. SACK and window scaling are agreed in the SYN.

Reliability · Going deeper · 2 of 3

Throughput can never beat the window divided by the round trip

A 65,535-byte window allows at most window ÷ round trip:1 ms round tripsame building524 Mbps10 ms round tripsame city52.4 Mbps40 ms round tripacross a continent13.1 Mbps100 ms round tripacross an ocean5.2 Mbpsthe 1 Gbps linkA sender may have only one window of unacknowledged data in flight, so long round trips cap the speed.The receiver advertises how many bytes it can take; the sender waits for ACKs once that much is in flight. With the 16-bitfield’s 65,535 bytes, a 100 ms path tops out near 5.2 Mbps whatever the link speed. Window scaling raises thelimit: filling 1 Gbps over 40 ms needs 5 MB in flight, the bandwidth-delay product.

Throughput ≤ window ÷ round trip: 65,535 bytes over 100 ms is 5.2 Mbps. Window scaling lifts the limit.

Reliability · Going deeper · 3 of 3

Congestion control: probe, back off, probe again

segments in flight012345678910111213round trips10204080160slow start: doubles each round trip✗ loss: halvethen +1 segment each round trip80TCP probes for spare capacity, doubling until loss, then halving and growing slowly; this is congestion control.It starts with 10 segments (RFC 6928), doubles each round trip, and treats a loss as a sign of a full queue. Real stacksdiffer in the details: Linux uses CUBIC and cuts to 0.7, BBR measures bandwidth and delay instead. The sender uses the smallerof this congestion window and the receiver’s window.

Slow start doubles the congestion window each round trip; a loss halves it, then it grows by one segment per round trip.

UDP

UDP sends and forgets, so it is quick and light

Header size, to scaleTCP 20 bytes (up to 60)UDP 8 bytesone DNS lookup, 40 ms round tripover TCP: 80 msover UDP: 40 msRuns over UDPDNSport 53DHCPport 67, 68TFTPport 69NTPport 123SNMPport 161, 162Syslogport 514Voice and video (RTP)port dynamicQUIC, HTTP/3port 443UDP sends each message on its own: no handshake, no acknowledgement, no resending, and only 8 bytes of header.Its header holds just ports, a length and a checksum. That suits short questions and answers, where one lost packet is simplyasked again, and live media, where a late packet is useless. Applications that need some reliability build it themselves:QUIC runs over UDP with its own handshake, acknowledgements and encryption, and carries HTTP/3.

UDP has no handshake, acknowledgement or resend and an 8-byte header; DNS, DHCP, NTP, SNMP, voice and QUIC use it.

UDP · Going deeper · 1 of 3

TCP and UDP side by side

TCPUDPConnectionthree-way handshake firstnone, just sendDeliveryevery byte, in orderbest effort, any orderLost dataresent automaticallygone unless the app asks againFlow and congestion controlwindow and congestion windownoneHeader20 to 60 bytes8 bytesSendsa byte streamseparate datagramsMulticast and broadcastnoyesTypical usesweb, email, SSH, file transfer, databasesDNS, DHCP, NTP, SNMP, voice, video, games, QUICTCP trades a little delay for completeness; UDP leaves reliability to the application.DNS uses UDP for lookups and TCP for zone transfers and answers too big for one packet. QUIC shows the trend: the reliabilityof TCP rebuilt above UDP, so it can be changed in the application instead of in every operating system.

TCP gives an ordered, complete byte stream; UDP gives separate datagrams, best effort, with multicast and broadcast.

UDP · Going deeper · 2 of 3

Live voice cannot wait for a resend

One voice packet every 20 ms: 160 bytes of G.711 audio plus RTP 12, UDP 8 and IP 20 = 200 bytesaudio 160RTP 12UDP 8IP 20× 50 a second = 80 kbps of IP (87.2 on Ethernet)sentarrived✗ lostMeasureVoice needsIf notDelay, one wayunder 150 ms (ITU-T G.114)people talk over each otherJitterunder 30 msthe jitter buffer adds delayLossunder 1%clipped wordsLive voice cannot wait for a resend, so it runs over UDP and the network must keep delay, jitter and loss low.Packets leave every 20 ms but arrive unevenly; the phone holds a short jitter buffer to play them smoothly and covers a lost one.A resend would come a round trip too late. Quality of service gives these packets the front of the queue when links are busy.

A G.711 call sends 200-byte packets 50 times a second, 80 kbps. Voice needs under 150 ms latency, 30 ms jitter, 1% packet loss.

UDP · Going deeper · 3 of 3

Quality of service: mark, queue, shape

1 Classify and mark: DSCP, 6 bits in the IP headerEF46 = 101110voiceAF4134 = 100010video callsCS324 = 011000call signallingAF2118 = 010010business dataDefault0 = 000000everything else2 Queue: voice first, the rest shares by weightpriority queue (EF), cappedvideo 30%data 40%default 30%busy uplink3 Police or shape at a rate limit✗policing drops (or re-marks) the excess✓shaping queues the excess and sends it laterQoS sorts packets by a mark in the IP header and gives each class its own queue when a link is congested.Mark as close to the source as you trust (the trust boundary, often the IP phone or access switch). WRED drops some low-prioritypackets early so TCP slows before the queue overflows. QoS does nothing on an idle link: it only decides who waits.

QoS marks packets with DSCP (voice is EF, 46), queues each class separately and polices or shapes at rate limits.

Lesson complete

You finished “TCP and UDP”

Nicely done.

Open in a new tab