Skip to lesson

TCP: the connection under HTTP

A checkout page sometimes hangs for half a minute and sometimes fails at once. Both are network problems that break in different places. To tell them apart, learn what happens before the first byte leaves.

Network layers in one picture

A packet is a small chunk of data that the network carries from one address to another. Packets can get lost, copied or arrive out of order.

The first example shows every layer in one curl -v run: the address lookup, the connection, the encryption, then the request.

Four layers, each one riding on the layer below. At the top, HTTP is the request and response text. Under it, TLS encrypts the stream and is used only for https. Under that, TCP turns packets into a reliable, in-order stream of bytes between two programs.

Example 1 · Every layer in one curl -v run

Terminal
curl -sv https://example.com -o page.html
Sample output (curl on macOS or Linux, trimmed; Windows curl prints fewer TLS lines)
* Host example.com:443 was resolved.
* IPv6: 2606:4700:10::6814:179a, 2606:4700:10::ac42:93f3
* IPv4: 172.66.147.243, 104.20.23.154
...
*   Trying 172.66.147.243:443...
* ALPN: curl offers h2,http/1.1
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
...
* TLSv1.3 (IN), TLS handshake, Server hello (2):
...
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / x25519 / id-ecPublicKey
* ALPN: server accepted h2
...
* Connected to example.com (172.66.147.243) port 443
* using HTTP/2
...
> GET / HTTP/2
> Host: example.com
> User-Agent: curl/8.14.1
> Accept: */*
>
< HTTP/2 200
< content-type: text/html; charset=utf-8
...

What this code does

  • resolved is DNS, Trying 172.66.147.243:443 is IP plus TCP (the handshake to that address and port), the TLS handshake lines are TLS, and the > and < lines are HTTP. Each layer finishes before the next starts.
  • ALPN is how client and server agree on the HTTP version during the TLS handshake: here they picked h2, HTTP/2, so the request line says HTTP/2. The Windows curl used in the other examples of this module speaks only HTTP/1.1; the request means the same thing either way.

A web request is split into layers. Each layer does one job and relies on the layer below, like a letter inside an envelope inside a courier bag:

  • IP carries the packets from one address to another.
  • TCP, on top of IP, turns the packets into a reliable, in-order stream of bytes between two programs (address plus port on each side).
  • TLS, on top of TCP, encrypts the stream. It is used for https and skipped for plain http; the next lesson covers it.
  • HTTP, on top, is the actual request and response text.
This is a simplified picture of the layers that matter for web work. Textbooks show more layers (the OSI model has seven), but these four explain every web error you will meet.

TCP: a reliable byte stream, opened with a handshake

TCP (Transmission Control Protocol) gives two programs a connection: whatever bytes one side writes, the other side reads, complete and in the same order.

The example measures how long the handshake takes before any HTTP is sent.

Example 2 · The handshake costs one round trip

Terminal
# The handshake:#   client  ---- SYN -------->  server#   client  <--- SYN-ACK -----  server     (1 round trip)#   client  ---- ACK -------->  server     then HTTP can startping -n 3 github.comcurl -s -o page.html -w "dns lookup:  %{time_namelookup}s\ntcp connect: %{time_connect}s\ntls done:    %{time_appconnect}s\nfirst byte:  %{time_starttransfer}s\ntotal:       %{time_total}s\n" https://github.com/
Sample output (Windows; macOS uses ping -c 3; your times differ)
...
Approximate round trip times in milli-seconds:
    Minimum = 38ms, Maximum = 52ms, Average = 44ms
dns lookup:  0.033346s
tcp connect: 0.075277s
tls done:    0.134958s
first byte:  0.184630s
total:       0.500958s

What this code does

  • -w prints timings after the request. Each one counts from the start, so the TCP handshake took 0.075 - 0.033 = 0.042 s, about 42 ms.
  • ping measured one round trip to github.com at about 44 ms on average. The handshake took about the same: one round trip, exactly as SYN, SYN-ACK, ACK predicts.
  • TLS took another 60 ms and the server needed 50 ms more before the first byte of the response. The next lessons and the dev tools Timing view use the same phases.

TCP numbers every byte, the receiver confirms what arrived, and anything lost is sent again. Without it, a web page could arrive with missing or jumbled parts.

A connection starts with the three-way handshake: the client sends SYN ("let us talk"), the server answers SYN-ACK ("ok, let us talk"), and the client sends ACK ("ok"). This costs one round trip, the time for a message to go to the server and back, before any HTTP is sent, so a far-away server is slow to connect to even when it is fast at answering. If the computer is reached but no program waits on that port, it answers the SYN with a refusal at once: the "connection refused" error from the DNS lesson.

HTTP is just text in the stream

Once the connection is open, an HTTP/1.1 request is plain text. The next two examples type one by hand into a raw connection.

Example 3 · Type an HTTP request by hand

Terminal
printf 'GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | curl telnet://example.com:80
Sample output (trimmed; run in Git Bash or macOS Terminal)
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:27:39 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Server: cloudflare
...

241
<!doctype html><html lang=en><head><meta charset=utf-8>...<title>Example Domain</title>...</html>

0

What this code does

  • printf writes three lines, each ending in \r\n, plus the empty line that ends the request. curl telnet://example.com:80 opens a plain TCP connection to port 80 and sends those bytes as they are.
  • The server answered with ordinary text: a status line, header lines, an empty line, then the page. Connection: close asked it to close the connection afterwards, so curl stops by itself.
  • 241 and 0 are part of Transfer-Encoding: chunked: the body is sent in pieces, each starting with its size in hex (0x241 = 577 bytes); 0 marks the end. curl and browsers remove these numbers for you.

Example 4 · Forget the Host header

Terminal
printf 'GET / HTTP/1.1\r\nConnection: close\r\n\r\n' | curl telnet://example.com:80
Sample output (trimmed)
HTTP/1.1 400 Bad Request
Server: cloudflare
Date: Fri, 16 Oct 2026 02:28:24 GMT
Content-Type: text/html
Content-Length: 155
Connection: close
...

What this code does

  • The TCP connection worked; only the HTTP text was wrong. The server could not tell which of its many sites you wanted, so it answered 400 Bad Request.
  • This is the difference between layers in practice: a TCP problem gives you no response at all, while an HTTP problem still gets a response, just an error one.

A request is a request line (GET / HTTP/1.1), then header lines such as Host: example.com, then one empty line that says "the request ends here". Each line ends with the two characters carriage return and line feed, written \r\n. The response comes back the same way. The HTTP Requests & Responses module explains every part.

Because it is just text, you can type a request yourself: curl telnet://host:port opens a plain TCP connection and passes your text through without adding anything.

Find the bug

Find the broken request line

Each raw request gets 400 Bad Request. Click the line with the mistake.

Which line is wrong?

Round 1 of 3
The Host: header is required in HTTP/1.1. One server address often hosts many sites, and Host says which one you want. Leave it out and servers answer 400 Bad Request.

Keep-alive, and HTTP/1.1 versus HTTP/2 and HTTP/3

Keep-alive keeps a TCP connection open after a response, so the next request reuses it instead of paying for another handshake. It is the default in HTTP/1.1.

The example sends two requests in one curl command and shows that the second one reuses the first connection.

Example 5 · Keep-alive: two requests, one connection

Terminal
curl -sv https://example.com/ https://example.com/ -o first.html -o second.html
Sample output (Windows curl, trimmed)
*   Trying [2606:4700:10::ac42:93f3]:443...
...
> GET / HTTP/1.1
> Host: example.com
...
< HTTP/1.1 200 OK
...
* Connection #0 to host example.com:443 left intact
* Reusing existing https: connection with host example.com
> GET / HTTP/1.1
> Host: example.com
...
< HTTP/1.1 200 OK
...
* Connection #0 to host example.com:443 left intact

What this code does

  • left intact means curl kept connection number 0 open after the first response. For the second URL it printed Reusing existing https: connection: there is only one Trying line, so only one TCP handshake and one TLS handshake were done.
  • Browsers do the same for every file of a page. Reusing connections is one of the biggest speed savings on the web.

A page that needs 30 files reuses a few connections instead of opening 30.

HTTP/1.1 (1997) sends text and handles one request at a time on a connection. HTTP/2 (2015) sends the same requests and responses as binary frames and runs many at once over one TCP connection. HTTP/3 (2022) runs over QUIC, a newer transport on top of UDP instead of TCP, which connects faster and copes better with bad mobile networks. The meaning of requests, headers and status codes is the same in all three, which is why the rest of this course mostly shows HTTP/1.1.

Timeouts and "connection reset": what failed and where

A timeout means the client gave up waiting for an answer. A connection reset means the other side ended the connection abruptly in the middle.

This example shows three failures at the TCP level: refused, timed out and reset.

Example 6 · Three failures at the TCP level

Terminal
curl http://localhost:8000/curl --connect-timeout 5 http://10.255.255.1/curl http://127.0.0.1:18555/orders
Sample output (Windows curl; the third line needs a test server that resets connections)
curl: (7) Failed to connect to localhost:8000 after 2239 ms: Could not connect to server
curl: (28) Connection timed out after 5005 milliseconds
curl: (56) Recv failure: Connection was reset

What this code does

  • Error 7: no server on port 8000, so the handshake was refused. (Windows retries a refused connection twice, which is why it took about 2 seconds; macOS fails in a few milliseconds.) Error 28: 10.255.255.1 is a private address with no machine behind it here, so the SYN was never answered and curl gave up after the 5 seconds allowed by --connect-timeout.
  • Error 56: the third address ran a small test server that accepted the connection, read the request, and then reset the connection instead of answering. A crashing server looks exactly like this. On macOS and Linux the text is Connection reset by peer.

A connect timeout means the handshake never got an answer: the address is wrong, the machine is off, or a firewall silently drops the packets. A timeout after connecting means the server accepted the connection but did not answer in time, usually because it is overloaded or stuck. A connection reset is a TCP reset (RST) sent instead of a normal close; typical causes are a server program that crashed while handling the request, or a proxy or firewall in between that cut it off.

Each error points to a different next step:

  • Refused (curl 7): reached the machine; nothing on that port. Start the server or fix the port.
  • Connect timeout (curl 28): no answer at all. Check the address, the network and firewalls.
  • Reset (curl 56, "Connection reset by peer" on macOS and Linux): connected, then cut off. Look at the server's logs.

Next step

Check your understanding

12 quick questions