Networking
TCP/IP Explained: The Four Layers Behind Every Server Connection
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيTCP/IP is the family of protocols that the internet runs on. Whenever a visitor opens your website, a bot calls your API, or you connect to your VPS over SSH, data is packaged, addressed, sent, reassembled and acknowledged according to rules defined by this suite. You do not need to memorize RFCs to run a server, but a working mental model of the layers helps you understand error messages, firewall behavior and performance tuning. This article walks through the model with a server owner's point of view.
The four layers of the TCP/IP model
The TCP/IP model groups networking into four layers. Each layer offers a service to the one above and relies on the one below.
| Layer | Job | Examples |
|---|---|---|
| Application | The data your programs care about | HTTP, DNS, SSH, SMTP, TLS |
| Transport | Delivery between programs, using ports | TCP, UDP, QUIC (over UDP) |
| Internet | Addressing and routing between networks | IPv4, IPv6, ICMP |
| Link | Moving frames on the local network | Ethernet, Wi-Fi, ARP |
You will also hear about the seven-layer OSI model. It is a teaching reference; TCP/IP is what is actually implemented. People still say "layer 4 load balancer" (transport) or "layer 7 proxy" (application) using OSI numbering.
Encapsulation: envelopes inside envelopes
When your web server sends a response, each layer wraps the data with its own header:
- The application produces an HTTP response, usually inside a TLS record.
- TCP splits the stream into segments and adds source and destination ports, sequence numbers and flags.
- IP adds source and destination addresses and a hop limit (TTL in IPv4, Hop Limit in IPv6).
- The link layer adds a frame header with hardware addresses for the next hop only.
Routers in the middle look mainly at the IP header and replace the link layer header at every hop. The receiving server unwraps the layers in reverse order. The maximum size of a packet on a link is the MTU, typically 1500 bytes on Ethernet. When packets exceed it along a path and the signals that would warn the sender are blocked, you get the classic symptom of small requests working while large transfers hang.
TCP: reliable, ordered streams
TCP gives applications the illusion of a continuous, reliable byte stream over an unreliable network. It does this with:
- Sequence numbers and acknowledgments, so the receiver can reorder data and the sender knows what arrived.
- Retransmission of segments that were not acknowledged in time.
- Flow control, where the receiver advertises how much data it can buffer.
- Congestion control, where the sender adjusts its speed to the capacity of the path. Algorithms such as CUBIC and BBR live here.
The three-way handshake
Before data flows, client and server agree on starting sequence numbers:
- Client sends SYN.
- Server replies SYN-ACK.
- Client sends ACK, and the connection is established.
This costs one round trip before the first byte of the request, which is why latency to your server matters so much, and why TLS 1.3 and HTTP/3 work hard to cut extra round trips. Connections close with FIN segments, or abruptly with RST. Seeing many sockets in TIME_WAIT after closing is normal for a busy server.
UDP: simple and fast
UDP sends independent datagrams with ports and a checksum, and nothing else: no handshake, no ordering, no retransmission. It suits workloads where speed matters more than perfect delivery or where the application handles reliability itself: DNS queries, NTP, VoIP, online games, VPNs such as WireGuard, and QUIC, the transport under HTTP/3.
| Feature | TCP | UDP |
|---|---|---|
| Connection setup | Handshake required | None |
| Delivery guarantee | Yes, with retransmission | No |
| Ordering | Preserved | Not guaranteed |
| Overhead | Higher | Lower |
| Typical use | Web, SSH, email, databases | DNS, NTP, gaming, VPN, QUIC |
IP and ICMP
IP is a best-effort delivery service: it routes each packet independently and may drop, duplicate or reorder them. ICMP carries control messages such as "destination unreachable", "time exceeded" (which makes traceroute work) and echo requests used by ping. Blocking all ICMP on a server is a common mistake: it breaks path MTU discovery and makes troubleshooting harder. Rate-limit it if you must, but allow the essential types, especially for IPv6, which depends on ICMPv6 for neighbor discovery. Our guide on IP addresses covers the addressing side in detail.
Observing TCP/IP on your VPS
# Socket states and counts
ss -s
ss -tan state established | head
# Path and per-hop latency
ping -c 4 example.com
mtr -rwc 50 example.com
# Capture traffic on port 443 (Ctrl+C to stop)
sudo tcpdump -ni eth0 tcp port 443 -c 20
# Show only handshake packets (SYN or RST flags set)
sudo tcpdump -ni eth0 'tcp[tcpflags] & (tcp-syn|tcp-rst) != 0'
# Current congestion control algorithm
sysctl net.ipv4.tcp_congestion_controlReplace eth0 with your interface name from ip -br link. Packet captures can contain sensitive data, so store them carefully and delete them when you are done.
Reading common symptoms
- Connection refused: the host answered with RST; nothing listens on the port.
- Connection timed out: SYNs got no reply; usually a firewall or routing problem.
- Slow first byte but fast transfer: latency and handshakes dominate; consider a closer region or connection reuse.
- Throughput drops on long paths: packet loss or small buffers limit TCP's window.
Choosing a data center close to your users shortens every round trip. You can compare regions on our pricing page.
FAQ
Is TCP/IP the same as the internet?
TCP/IP is the protocol suite; the internet is the global network of networks that uses it. Private networks inside companies and cloud platforms also run TCP/IP without being part of the public internet.
Is UDP less secure than TCP?
Neither protocol encrypts data by itself. Security comes from layers above, such as TLS, DTLS, QUIC or WireGuard. UDP services can be abused for amplification attacks if they answer large replies to spoofed requests, so public UDP services need careful configuration.
Why does HTTP/3 use UDP instead of TCP?
HTTP/3 runs on QUIC, which is built on UDP so it can implement its own reliability, encryption and multiplexing in user space. This avoids head-of-line blocking across streams and allows faster connection setup, while UDP passes through existing networks without needing changes to operating system kernels.