What a TCP connection is
A TCP connection is a shared state between two programs on two computers. Both operating systems record it. It starts when the connection is set up and lasts until it is closed. That state lets a browser send a request and receive the reply in order, while the program itself never sees the packets the network lost along the way.
If the terms are new, what is the client-server model in a computer network is a good place to start. In most TCP connections, the client starts the conversation and the server waits for it.
How a connection is identified
Each end of a connection is a socket, which is an IP address combined with a port number. A connection is identified by four values: the source IP address and port, and the destination IP address and port. The IP address part of that is covered in what is an ip address and how does it work in a computer network, if you want the background.
Two browser tabs that load the same website use two different connections. They differ in the source port, which the operating system picks for each new connection, so both can run at the same time.
The three-way handshake
Setting up a connection takes three segments:
- The client sends a SYN segment with its starting sequence number.
- The server replies with SYN-ACK. It acknowledges the client’s number and sends its own.
- The client sends an ACK, and the connection is established.
Each side picks its starting sequence number at random, which makes the numbers harder to guess. The handshake takes one round trip, so data cannot flow until that trip is complete.
Moving data
Each segment carries a sequence number. The receiver sends acknowledgments that say which bytes it has, and the sender resends anything that is not acknowledged in time. The receiver’s window size limits how much unacknowledged data the sender may have in flight, and the sender applies its own congestion limit as well.
Closing a connection
Either side can start a close by sending a FIN. The other side acknowledges it, and when its own program is done, it sends a FIN too. Because each direction closes separately, one side can keep sending after the other has finished.
The side that closes first keeps the connection in a TIME-WAIT state for a while, so that late packets from the old connection are not taken for a new one. On Linux, this lasts 60 seconds.
A reset, sent as RST, ends a connection at once. A server sends one, for example, when a packet arrives for a port that nothing is listening on.
Connection states
| State | What it means |
|---|---|
| LISTEN | A server waits for incoming connections on a port |
| SYN-SENT | A client has sent SYN and is waiting for SYN-ACK |
| SYN-RECEIVED | A server has received a SYN and sent SYN-ACK |
| ESTABLISHED | The handshake is done, and data can flow |
| FIN-WAIT-1 and FIN-WAIT-2 | This side has sent its FIN and is waiting for the other side to close |
| CLOSE-WAIT | The other side has closed, and this side’s program has not closed yet |
| LAST-ACK | This side has closed after CLOSE-WAIT and waits for the final acknowledgment |
| TIME-WAIT | This side closed first and holds the connection briefly |
Seeing connections on Linux and Windows
On Linux, ss -tn lists established TCP connections with their local and remote addresses and states. ss -tan lists every TCP socket, including the listening ones. On Windows, netstat -ano -p TCP shows each connection, its state, and the process ID (PID) that owns it. In PowerShell, Get-NetTCPConnection returns the same information as objects you can filter.
What unusual numbers can mean
- A large number of TIME-WAIT sockets is common on busy servers that open and close many short connections. On its own, it is usually not a fault.
- Many CLOSE-WAIT sockets often point to a program that is not closing its connections when it is done with them.
- A connection that stays in SYN-SENT usually means the SYN never reached the server, or its reply never came back. A firewall or a routing problem is a common cause.
Many of these symptoms are among the problems in what are the 10 common network problems, so that list is a useful next step when a connection keeps failing.