What TCP does for applications
TCP, the Transmission Control Protocol, gives applications a reliable connection over an IP network. IP sends packets on a best-effort basis: they can be lost, duplicated, delayed, or arrive out of order. TCP handles those problems so that the application sees one ordered stream of bytes between two endpoints.
If you are new to addressing, read what is an ip address and how does it work in a computer network first, since TCP relies on IP to carry every segment.
The main services TCP provides are:
- Connection setup. Both sides agree to communicate before any data moves, using a three-way handshake.
- Reliable delivery. The receiver acknowledges the data it gets, and the sender resends anything that is not acknowledged in time.
- Ordering. Every byte has a sequence number, so the receiver can put segments back in the right order.
- Error checking. A checksum in each segment helps catch corrupted data.
- Flow control. The receiver tells the sender how much data it can accept, so a fast sender does not overwhelm a slow receiver.
- Congestion control. The sender slows down when it sees signs of congestion, such as lost packets.
- Multiplexing by port. Many connections share one IP address, and port numbers tell the operating system which program each segment belongs to.
How a TCP connection starts and ends
A client begins with a SYN segment, the server answers with SYN-ACK, and the client confirms with an ACK. This three-way handshake takes one round trip before the first byte of application data can flow.
When a side is finished sending, it sends a FIN, and the other side acknowledges it. Each direction closes on its own, so one side can keep sending after the other has finished. The side that closes first keeps its socket in a waiting state called TIME-WAIT for a short time. That stops late packets from the old connection being mistaken for a new one.
Ports: one address, many services
Each service listens on a port, a 16-bit number from 0 to 65535. Ports 0 to 1023 are the well-known ports, which are assigned to common services. Ports are a convention rather than a rule. A service can listen on any free port, and many do.
These are some common TCP ports:
| Service | TCP port |
|---|---|
| SSH (remote shell) | 22 |
| SMTP (sending mail) | 25 |
| DNS (large replies and zone transfers) | 53 |
| HTTP (web) | 80 |
| HTTPS (encrypted web) | 443 |
| IMAP over TLS (reading mail) | 993 |
| SMB (Windows file sharing) | 445 |
| RDP (Windows Remote Desktop) | 3389 |
DNS normally uses UDP port 53 for small queries. It switches to TCP for large answers and zone transfers. The application protocols behind these ports belong to the services provided by application layer, and TCP only carries their bytes reliably.
TCP compared with UDP
| TCP | UDP | |
|---|---|---|
| Setup | Handshake before data | None |
| Delivery | Acknowledged and resent when lost | Best effort, no resends |
| Order | Kept | Not kept |
| Header size | 20 bytes minimum | 8 bytes |
| Common uses | Web pages, email, SSH, file transfers | DNS queries, live video, online games |
Some newer protocols build their own reliability on top of UDP. QUIC, which carries HTTP/3, is one example. This is why TCP is not the only transport in use, even though it still carries a large share of web traffic.
Checking TCP services on Linux
ss -tlnp lists TCP sockets in the listening state, with numeric addresses and, when you have permission to see them, the program that owns each one. Run it with sudo to see every process. ss -tn shows established connections. Older guides use netstat -tlnp from the net-tools package, which is deprecated on most current distributions, though it may still be installed.
For a wider set of commands, see what are 10 common network commands.
Checking TCP services on Windows
In Command Prompt, netstat -ano -p TCP lists TCP connections and listening ports, with the process ID (PID) for each. In PowerShell, Get-NetTCPConnection -State Listen lists the listening ports and the process that owns each one.
Testing a port from another machine
On Linux, nc -zv host 443 tries to open a TCP connection to the port, if netcat is installed. In PowerShell, Test-NetConnection -ComputerName host -Port 443 does the same. Read the result carefully:
- Connected. Something is listening on the port, and the path to it is open.
- Connection refused. The host answered, but nothing accepted the connection on that port, or a firewall actively rejected it.
- Timeout. No answer came back. A firewall may be silently dropping packets, or the host or the route to it may be unreachable.
A timeout does not prove that a service is down. If the results do not make sense, the problems in what are the 10 common network problems are a good checklist.
Exposing a service from your own network
Self-hosting a TCP service at home, such as a web server or SSH, means your router or firewall has to let the port through. Port forwarding sends traffic arriving at your public address to a machine inside your network. Before you do it:
- Open only the ports you need.
- Keep the software updated, and use strong authentication.
- For administration, such as SSH, consider a VPN instead of a port open to the whole internet.
- Check whether your internet provider gives you a public IP address. Many providers share addresses through carrier-grade NAT, and that can block inbound connections completely.
TCP sits under most of the services you run, so knowing its handshake, header, and ports makes it easier to work out why a service will not start or accept connections.