The app only hands bytes to a socket The browser already knows the server's IP from DNS: 203.0.113.10. It asks the operating system for a TCP connection to port 443 and gets back a socket.
The app never builds packets itself. It writes bytes (an HTTP request) into the socket. The kernel's network stack does everything below that, layer by layer.
Each layer adds a header: segment → packet → frame Transport layer: TCP adds a header with ports. The destination port is 443, and the source port is a random ephemeral port such as 51514 (on Linux, from 32768–60999). Data plus TCP header is a segment.
Internet layer: IP adds source and destination IP addresses and a TTL (64 by default on Linux). Now it is a packet. Link layer: Ethernet adds source and destination MAC addresses. Now it is a frame, which goes on the wire.
Key idea Ports pick the program, IPs pick the host across networks, MACs pick the next device on this LAN. Each layer reads only its own header.
The routing table says: not local, send it to the gateway The kernel looks up 203.0.113.10 in its routing table. It is not inside 192.168.1.0/24, so the only match is the default route: via 192.168.1.1, the router.
The destination IP in the packet stays 203.0.113.10. The gateway is only the next hop. It decides where the packet goes next.
Key idea ip route shows this table. "default via ..." is the line that sends everything outside your subnet to the router.
ARP finds the gateway's MAC, and the switch delivers the frame A frame needs a destination MAC, but the client only knows the gateway's IP. It broadcasts an ARP request: "Who has 192.168.1.1?" The router answers with its MAC, and the client caches it in its neighbor table.
The frame goes out with the router's MAC as destination. The switch reads only that MAC and forwards the frame out the port where it learned that address.
Every router rewrites the frame and lowers the TTL Each router removes the frame, reads the IP header, lowers TTL by 1 and looks up its own routing table. It then wraps the packet in a new frame with new MACs for the next link. MAC addresses change at every hop.
The IP addresses normally stay the same end to end. One common exception is NAT: a home or office router rewrites the private source IP 192.168.1.20 to its public IP. If TTL reaches 0, the router drops the packet and sends back an ICMP Time Exceeded message. traceroute uses exactly this to list the hops.
Three-way handshake, then the kernel hands it to the process That first packet was actually a TCP SYN with no data. The server's kernel strips the frame and IP header and checks the TCP port: a socket is listening on 443, so it answers SYN-ACK. The client replies ACK. The connection is ESTABLISHED.
One full round trip passes before any HTTP byte is sent. On top of this, TLS adds its own handshake. The kernel queues the new connection, and nginx, the process holding the listening socket, accepts it.
Sequence numbers and ACKs make a reliable stream Now the HTTP bytes flow. Each segment carries a sequence number, and the receiver acknowledges what it got with an ACK. If a segment is lost, the sender notices (no ACK before the timer, or repeated duplicate ACKs) and sends it again.
The app sees one ordered byte stream and never sees the loss, only the delay. With Ethernet's 1500-byte MTU, minus 20 bytes of IP and 20 of TCP header, one segment carries at most 1460 bytes of data.
Key idea Packet loss rarely shows as an error. It shows as slowness, because TCP keeps resending.
Refused comes back at once, timeout never comes back Port 8080 on the server: nothing listens there. The server's kernel answers the SYN with RST. curl -v shows "Connection refused" immediately. The host is reachable. The app is not running, or listens on another port or address (for example only 127.0.0.1).
Port 22: the firewall silently drops the SYN. No answer comes, so the client resends the SYN with growing gaps. With Linux defaults it gives up after about two minutes, and you see "timed out". Something on the path is dropping traffic. Some firewalls reject instead of dropping, and then it looks like refused.
Watch out Refused: you reached the host, check the process and its listen address. Timed out: check the path, meaning firewalls, security groups and routes.
Small packets pass, big ones vanish: the MTU black hole A VPN, tunnel or overlay network adds its own header, so the path MTU drops below 1500. A full-size packet with "Don't Fragment" set cannot pass. The router should send back ICMP "Fragmentation Needed", and the sender then shrinks its packets.
If a firewall blocks that ICMP, the sender never learns. The handshake and small requests work, but large responses hang. Lowering the interface MTU, or clamping TCP MSS on the tunnel, fixes it.
Watch out Connects fine, small requests fine, large downloads stall? Suspect MTU. Test with ping -M do -s 1472 (Linux).
Wrap on the way down, unwrap on the way up The sender wraps data in TCP (ports), IP (IP addresses, TTL) and Ethernet (MAC addresses). The routing table picks the next hop, ARP finds its MAC, and each router unwraps and rewraps the frame. The receiver unwraps it again and hands the bytes to the process listening on the port.
TCP adds a handshake, sequence numbers and resending on top, so the app gets a reliable stream. Kubernetes networking builds on these same layers: Pod IPs, Services and NetworkPolicies are rules on packets.
Summary Debug bottom-up: is there a route (ip route), a neighbor (ip neigh), a path (traceroute), a listener (ss -tlnp), and a response (curl -v)?