Sub topic 6
Opening a TCP connection
Once DNS has given the browser an IP address, the browser needs a channel to the server. This chapter assumes you know what an IP address and a port are, and that you have seen the URL and DNS stages covered earlier. Here you will see how TCP sets up that channel and what can go wrong.
What TCP provides
TCP (Transmission Control Protocol) runs on top of IP. IP delivers packets on a best-effort basis. Packets can be lost, duplicated or arrive out of order. TCP adds a reliable, ordered byte stream on top of that. The application writes bytes in and the same bytes come out the other end, in order.
To do this, TCP keeps state at both ends. Before any data moves, the two sides must agree on starting values. That agreement is the connection setup.
The destination: IP plus port
A connection is identified by four values: source IP, source port, destination IP and destination port. The browser takes the destination IP from DNS. The destination port comes from the URL, or from the scheme's default when the URL has none. Port 443 is the default for https: and port 80 for http:. The operating system picks a temporary source port for the browser.
The three-way handshake
Each side must tell the other which sequence number it will start counting bytes from. The exchange takes three messages.
- SYN. The browser sends a segment with the SYN flag and its initial sequence number
x. - SYN-ACK. The server answers with its own initial sequence number
y. It also acknowledgesx+1. - ACK. The browser acknowledges
y+1. Both sides now consider the connection open.
The browser can send data right after its ACK. The cost is one round trip (RTT) before the first byte of the request leaves. If the server is 80 ms away, setup costs about 80 ms.
What the operating system does
Your browser does not build these packets. It asks the operating system for a socket and calls connect. The kernel sends the SYN and waits. If no SYN-ACK arrives, it retransmits with growing delays, so an unreachable host can take many seconds to fail.
You can watch a connect from a shell. This command uses a bash feature to open a TCP connection and reports whether it worked.
timeout 5 bash -c 'exec 3<>/dev/tcp/example.com/443' \
&& echo "TCP connect succeeded" \
|| echo "TCP connect failed or timed out"Common errors
| Symptom | Likely cause |
|---|---|
| Connection refused | The host is reachable, but nothing listens on that port. The server sent a reset (RST) in reply to the SYN. |
| Connection timed out | No reply came back. A firewall may be dropping packets, or the host is down or unreachable. |
| Slow first request | High RTT, or SYN packets lost and retransmitted. |
| Connection reset | One side closed abruptly, often a proxy, firewall or crashed server. |
Where you see it in DevTools
In the Network panel, the timing breakdown for a request shows an "Initial connection" segment. It covers the TCP handshake, and for HTTPS it also includes the TLS time shown as "SSL". With curl -v, the line * Connected to example.com marks the moment the handshake completes.
Exercise
A server is 60 ms away by round trip. You open a new HTTPS connection to it. How long does the TCP handshake take before the browser can send data, and why does a second request on the same connection avoid this cost?
Show answer
About 60 ms, which is one round trip: SYN goes out, SYN-ACK comes back, and the browser can send data along with or right after its final ACK. A second request reuses the already open connection (keep-alive), so no new handshake is needed.
Exercise
You run the bash connect test against a host and get "connection failed" instantly, not after 5 seconds. Is this more likely a refused connection or a firewall dropping packets? Why?
Show answer
A refused connection. The server replies with a reset right away, so the failure is immediate. A firewall that silently drops packets gives no reply, so the attempt waits until the timeout.