Sub topic 4
Reaching the server: IP and TCP connections
This chapter assumes you know what a URL is and that DNS turns a hostname into an IP address. It also assumes you can run commands in a terminal. Here you will see what happens after DNS returns an address and before the first HTTP byte is sent.
IP addresses and ports
An IP address identifies a machine on a network, such as 93.184.216.34 (IPv4) or 2606:2800:220:1:248:1893:25c8:1946 (IPv6). The Internet Protocol (IP) breaks data into packets and routes each one hop by hop toward that address. IP gives no guarantee that a packet arrives, arrives once, or arrives in order.
One machine runs many programs, so an address is not enough. A port is a number from 0 to 65535 that selects a program on that machine. A connection is identified by four values: client IP, client port, server IP, and server port. Browsers default to port 80 for http and 443 for https when the URL has no port.
TCP: reliable streams over unreliable packets
The Transmission Control Protocol (TCP) sits on top of IP. It gives your program a reliable, ordered stream of bytes. To do this, TCP numbers the bytes, acknowledges what arrives, and resends what is lost. HTTP/1.1 and HTTP/2 both run on TCP. HTTP/3 uses a different transport, which a later chapter covers.
Before any data flows, the two sides set up the connection with a three-way handshake.
The handshake costs one round trip before the request can be sent. On a path with 80 ms round-trip time, that is 80 ms of waiting. This is one reason browsers reuse connections instead of opening a new one for every request.
Seeing a connection
This Node.js script opens a TCP connection by hand and writes an HTTP request into it. Save it as connect.mjs and run node connect.mjs.
import { connect } from "node:net";
const socket = connect({ host: "example.com", port: 80 }, () => {
console.log("Connected to", socket.remoteAddress, "port", socket.remotePort);
socket.write("GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n");
});
socket.on("data", (chunk) => process.stdout.write(chunk));
socket.on("end", () => console.log("\nServer closed the connection"));
socket.on("error", (err) => console.error("Failed:", err.message));The connect call performs DNS lookup and the handshake. The callback runs only after the handshake succeeds. HTTP is just text written into the stream.
Common connection errors
| Symptom | Likely cause |
|---|---|
| `ECONNREFUSED` (connection refused) | The machine answered, but no program listens on that port. |
| Timeout | Packets are dropped, often by a firewall, or the host is down. |
| `ECONNRESET` (connection reset) | The other side closed abruptly, for example after a crash. |
Refused connections fail fast because the server's operating system replies with a reset. Timeouts are slow because nothing replies at all. That difference helps you tell a stopped service from a blocked network path.
Exercise
A request to a server fails immediately with "connection refused". Another request to a different server hangs for 30 seconds and then times out. What does each result suggest?
Show answer
Connection refused means the host was reachable but nothing listens on that port, so the service is probably stopped or the port is wrong. The timeout means packets got no reply, which suggests a firewall dropping traffic, a wrong address, or a host that is down.