Skip to content

TCP vs UDP: the three-way handshake, ports, and what a closed port sends back

TCP and UDP captured on a lab link: the three-way handshake with its real sequence numbers, a closed port's reset, the 8-byte UDP header, and what Nmap reads.

9 min read

The Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP) are the two ways a program hands data to the network, and they sit at the transport layer, one step above IP. TCP opens a connection first, numbers every byte and resends what goes missing. UDP sends one datagram and forgets it.

The packets below come from three captures on a lab link where every address is from a documentation range: a web request over TCP, a refused TCP connection, and a DNS query over UDP. To list the TCP and UDP ports open on your own machine, skip to Check yours on Linux and Windows.

TCP vs UDP at a glance

TCPUDP
Defined inRFC 9293 (2022, replacing RFC 793)RFC 768 (1980)
Connection before dataYes, the three-way handshakeNo
DeliveryEvery byte acknowledged, lost data resent, delivered in orderSent once, no acknowledgement, no ordering
Header size20 bytes minimum, 40 with common options8 bytes, always
A closed port answers withA TCP resetAn ICMP port unreachable message
Typical usesWeb pages, remote shells, emailDNS lookups, clock sync, voice and video calls, HTTP/3

RFC stands for Request for Comments, the series the internet's protocols are published in.

Ports: how a packet finds the right program

An IP address gets a packet to a machine. A port gets it to one program on that machine.

Both TCP and UDP put two 16-bit port numbers at the very start of their header, a source and a destination, so a port is a number from 0 to 65,535. The Internet Assigned Numbers Authority (IANA) splits that range in three in RFC 6335:

RangeNameWhat lives there
0 to 1023System ports (well-known ports)Servers: 53 for DNS, 443 for HTTPS, 22 for SSH
1024 to 49151User portsRegistered services: 3306 for MySQL, 8080 for alternate HTTP
49152 to 65535Dynamic portsNever assigned, free for temporary use

The client side picks a temporary source port of its own for each connection. Linux doesn't use IANA's dynamic range for that, and two commands show both sides of a port on your own machine:

See the range Linux picks client ports from.

bash
sysctl net.ipv4.ip_local_port_range
text
net.ipv4.ip_local_port_range = 32768	60999

sysctl reads a kernel setting by name. Every client port in the captures below falls inside this range: 35314 for the web request, 52394 for the DNS query.

See which ports are waiting for connections.

bash
netstat -tuln
text
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN
udp        0      0 127.0.0.53:53           0.0.0.0:*

-t and -u pick TCP and UDP, -l shows only listening ports, and -n prints numbers instead of service names. Both lines are this laptop's local DNS resolver on port 53, once for each protocol; other lines are cut. UDP has no LISTEN state, because there is no connection to wait for.

A TCP port and a UDP port with the same number are unrelated. Port 53 over UDP and port 53 over TCP are two different doors, as the two lines above show, and a firewall rule for one doesn't touch the other.

The TCP three-way handshake

Before TCP sends a byte of data, the two sides exchange three segments to agree that a connection exists and which numbers each side will count from.

  1. The client sends a synchronize (SYN) segment, carrying its starting sequence number.
  2. The server answers SYN, ACK: its own starting number, and an acknowledgement (ACK) of the client's.
  3. The client sends ACK, acknowledging the server's number.

Here are those three on the lab link, curl connecting to a web server on port 443:

Wireshark filtered to tcp.port == 443 && frame.number <= 9, showing three packets. Frame 7 from 192.0.2.10 to 192.0.2.53: 35314 to 443 SYN, Seq=0. Frame 8 from 192.0.2.53 to 192.0.2.10: 443 to 35314 SYN, ACK, Seq=0 Ack=1. Frame 9: 35314 to 443 ACK, Seq=1 Ack=1. Red callouts mark the three packets and the TCP layer line of frame 8: Src Port 443, Dst Port 35314, Seq 0, Ack 1, Len 0
The three-way handshake: SYN, SYN-ACK, ACK. Marked in red: the three packets, and the server's reply with its sequence and acknowledgement numbers. Every address is from RFC 5737 and RFC 7042 documentation ranges

The whole handshake took 54.5 microseconds, from the SYN at 0.876156904 seconds to the ACK at 0.876211421. That's two virtual interfaces on one laptop. Across the internet the same exchange costs a full round trip to the server before any data can be sent.

The handshake carries no data, so Len=0 on all three. Frames 5 and 6 of the same capture, just before the SYN, are the client using the Address Resolution Protocol (ARP) to find the server's media access control (MAC) address, because the SYN can't be put in an Ethernet frame without it.

The SYN and the SYN-ACK also carry options, each side announcing its own. MSS=1460 is the maximum segment size, the most data that side will accept in one segment: a 1500-byte Ethernet payload minus a 20-byte IP header and a 20-byte TCP header. Those options are why frame 8's TCP header is 40 bytes long and not 20.

Sequence and acknowledgement numbers

Wireshark shows Seq=0 and Ack=1, but those are relative numbers it works out for readability. The real ones on the wire are large and random:

bash
tshark -r tls-handshake.pcapng -Y 'tcp.port == 443 && frame.number <= 11' \
  -T fields -e frame.number -e tcp.flags.str -e tcp.seq_raw -e tcp.ack_raw -e tcp.len
text
7	··········S·	3231802819	0	0
8	·······A··S·	2138397175	3231802820	0
9	·······A····	3231802820	2138397176	0
10	·······AP···	3231802820	2138397176	1568
11	·······A····	2138397176	3231804388	0

tshark is Wireshark's command-line version: -r reads a saved capture, -Y applies a display filter, and -T fields with each -e prints only the fields named. The columns are the frame, its flags (S for SYN, A for ACK, P for push), the raw sequence number, the raw acknowledgement number and the bytes of data. The client started counting at 3231802819. The server acknowledged 3231802820, one more, because a SYN uses up one sequence number even though it carries no data. The client did the same for the server's 2138397175.

Frame 10 is the first data: 1,568 bytes, the ClientHello that opens a Transport Layer Security (TLS) session. The server's acknowledgement in frame 11 is 3231804388, which is exactly 3231802820 plus 1568. That's all an acknowledgement is: the number of the next byte the receiver expects. If a segment goes missing, the acknowledgement stops moving, and the sender resends from there.

The starting number is random on purpose. RFC 9293 requires a clock-driven starting number and recommends adding a pseudorandom function of the two addresses, the two ports and a secret key. That function must not be computable from outside, it says, or an attacker could still guess at sequence numbers. RFC 5961, which RFC 9293 points to, describes what a good guess buys: resetting somebody else's connection, or injecting data into it, without ever seeing its packets.

Closing a TCP connection: FIN

A connection ends with a finish (FIN) segment in each direction, each one acknowledged. In the capture the server closed first (F is FIN):

bash
tshark -r tls-handshake.pcapng -Y 'tcp.flags.fin == 1 || frame.number == 24' \
  -T fields -e frame.number -e ip.src -e ip.dst -e tcp.flags.str
text
22	192.0.2.53	192.0.2.10	·······A···F
23	192.0.2.10	192.0.2.53	·······A···F
24	192.0.2.53	192.0.2.10	·······A····

The textbook picture has four segments, two FINs and two ACKs. This one has three, because the client acknowledged the server's FIN and sent its own in the same segment.

From the first SYN to that last ACK, one HTTPS request took 18 TCP segments.

A closed TCP port: RST

When nothing is listening on a TCP port, the machine still answers. It sends a reset (RST), which kills the attempt before it becomes a connection.

This capture is a DNS client trying DNS over TLS on port 853 against a server that only answers plain DNS, the same refusal covered in the DNS over TLS post:

Wireshark filtered to tcp.port == 853, showing six packets: three SYNs from 192.0.2.10 to 192.0.2.60 on port 853, each answered by RST, ACK from 192.0.2.60. Red callouts mark the first SYN and its RST, ACK reply, and the TCP layer line of the reply: Src Port 853, Dst Port 51178, Seq 1, Ack 1, Len 0
A SYN to a port with nothing listening, answered by RST, ACK, three times over as the client retries. Marked in red: the first attempt and its reset. Addresses are from RFC 5737

The reset came back 21.6 microseconds after the SYN. The client tried three times, each from a new source port, and got the same answer each time.

A TCP header with no options is 20 bytes, and this reset is exactly that: the status bar reads Transmission Control Protocol (tcp), 20 bytes.

The UDP header: four fields, eight bytes

UDP has no handshake, no sequence numbers and no acknowledgements, so its header has room for very little. RFC 768 defines four fields of 16 bits each:

FieldSizeIn the query below
Source port2 bytes52394 (cc aa)
Destination port2 bytes53 (00 35)
Length2 bytes60, the 8-byte header plus 52 bytes of DNS
Checksum2 bytes0x848d
Wireshark filtered to udp.port == 53, showing two packets: frame 7, a standard query for example.com from 192.0.2.10 to 192.0.2.53, and frame 8, the response with A 203.0.113.10. The User Datagram Protocol layer of frame 7 is expanded: Source Port 52394, Destination Port 53, Length 60, Checksum 0x848d. Red callouts mark the two packets and the UDP header with its four fields. The hex pane highlights the eight header bytes cc aa 00 35 00 3c 84 8d
A whole DNS lookup over UDP: one query, one response. Marked in red: the two packets, and the UDP header, all four fields of it. The status bar reads 8 bytes. Addresses are from RFC 5737

That's the whole lookup. Two packets, where the HTTPS request above needed three segments just to say hello.

UDP leaves everything else to the program. A DNS client that gets no answer simply asks again, and that's cheaper for a short question than holding a connection open. QUIC, the transport under HTTP/3 (a name, not an acronym, as RFC 9000 points out), runs inside UDP datagrams and builds its own acknowledgements and retransmission on top.

A closed UDP port: ICMP port unreachable

UDP has no reset to send. RFC 1122, section 4.1.3.1, says what a host does instead:

If a datagram arrives addressed to a UDP port for which there is no pending LISTEN call, UDP SHOULD send an ICMP Port Unreachable message.

The Internet Control Message Protocol (ICMP) is the error channel of IP. "SHOULD" in an RFC means recommended, not required, and that gap matters to anyone scanning a network.

What a port scanner reads from this

A port scanner is a tool that sends the first packet of a conversation and reads the answer. Nmap's documentation lists exactly how it maps each answer to a port state.

For a TCP SYN scan (nmap -sS):

Answer to the SYNPort state
SYN, ACKopen
RSTclosed
Nothing, even after retransmissionsfiltered
An ICMP unreachable errorfiltered

Nmap never sends the final ACK. The SYN-ACK already said the port is open, which is why the documentation also calls it half-open scanning.

For a UDP scan (nmap -sU):

Answer to the datagramPort state
Any UDP responseopen
ICMP port unreachableclosed
Nothing, even after retransmissionsopen|filtered
Other ICMP unreachable errorsfiltered

UDP scanning is slow, and the reason is the ICMP message from the last section. Nmap's documentation describes Linux limiting destination unreachable messages to one per second on its test machine, and notes that at that rate a scan of all 65,536 ports takes more than 18 hours. Silence also can't tell an open UDP port from a firewall dropping the packet, which is why open|filtered exists at all.

Which protocol each service uses

ServiceTransportPort
DNSUDP, and TCP for large answers53
DNS over TLSTCP853
HTTPS (HTTP/1.1 and HTTP/2)TCP443
HTTP/3UDP, through QUIC443
Secure Shell (SSH)TCP22
Dynamic Host Configuration Protocol (DHCP)UDP67 and 68
Network Time Protocol (NTP)UDP123

Check yours on Linux and Windows

The ports your machine is listening on are the first thing a scanner pointed at it would find.

On Linux

Listening ports: netstat -tuln, then the Local Address column (what each column means). A 127.0.0.1 or 127.0.0.53 address answers only this machine; 0.0.0.0 answers the network.

Which program owns each port: sudo netstat -tulnp. -p adds a column with the process ID and name, and sudo lets it name processes run by other users.

The client port range: sysctl net.ipv4.ip_local_port_range.

On newer Linux systems ss -tuln takes the same flags and prints the same list.

On Windows

These run in Command Prompt. Nothing here was captured on Windows, so no output is shown.

Listening ports: netstat -an, then the TCP lines whose state is LISTENING. UDP lines have no state.

Which program owns each port: netstat -ano. The last column is the process ID, which Task Manager's Details tab lists by name.

The client port range: netsh int ipv4 show dynamicport tcp.

On macOS

netstat -an | grep LISTEN lists the listening TCP ports, and lsof -iTCP -sTCP:LISTEN adds the program that owns each one.