Socket Pair: The Two Ends of a Connection
A socket is an endpoint through which a program sends or receives data. In TCP networking, a socket pair means the two endpoints that identify one connection: one at each end of the conversation.
TCP, short for Transmission Control Protocol, is a set of rules for communication between programs. In its addressing system, each end combines an Internet Protocol (IP) address with a port number. The IP address identifies a network location; the port number helps direct traffic to the right service or conversation there.
Put both ends together, and you can distinguish one connection from another. The TCP specification calls this a pair of sockets.
There's also a programming operation named socketpair(). It creates two connected sockets in one call. That related name has a different, more specific meaning, which matters when moving from a network diagram to program code.
What Do Port Numbers Actually Do?
Four values tell you which conversation
Consider this invented TCP connection, using addresses reserved for documentation:
| End | IP address | Port |
|---|---|---|
| Client | 192.0.2.10 | 51000 |
| Server | 198.51.100.20 | 443 |
The client is the program requesting a service. The server is the program providing it. Here, the connection is identified by the combination of both addresses and both ports.
Now suppose another client connects to the same server address and port. Its endpoint differs, so the complete pair differs. The server doesn't need a different service port for every client.
Even one client computer can have separate connections to the same server endpoint, distinguished by their client ports. Looking only at the server's port would erase the information needed to tell those conversations apart.
Client and server describe their roles in establishing and serving a connection. They don't divide the connection into one end that may only send and another that may only receive. Both sides of a TCP connection can send data.
The listener keeps listening
A server first needs a way to accept new connections. In the socket programming interface documented for Linux, it does that through a listening socket.
When the program accepts a pending request, the operating system creates a separate connected socket for that exchange. The original listening socket remains available for further requests. Linux's accept documentation makes this separation explicit.
So the pair for an established exchange consists of the client's endpoint and the server's connected endpoint. The still-listening socket isn't the server's half of every ongoing conversation.
This distinction explains how a server can keep welcoming new clients while it talks to the ones already connected.
- Keep work, guest and smart-home gear apart with up to 32 VLANs on one small switch.
- Copy any port's traffic to your laptop with port mirroring and see what is on the wire.
- Set it up from a web page instead of learning a command line.
When the name appears as socketpair()
The POSIX socketpair operation, part of a standard programming interface, hands a program two already-connected sockets. It returns two file descriptors: numbers the program uses to refer to those sockets.
For a stream pair, data can travel reliably in both directions as a sequence of bytes, the small units in which computers store data. The caller doesn't first have to arrange a listening socket and a separate connection request to obtain the pair.
One useful application is communication between running programs on the same computer. On Linux, socketpair() works with particular kinds of sockets, including those for local communication, rather than ordinary internet TCP sockets; its manual lists the supported kinds.
A network connection can therefore have a socket pair without having been created by socketpair(). In a connection log, look for the two addresses and ports. In code, the parentheses signal an operation that creates the pair itself.
Source Ports: The Number Changes Roles When the Reply Returns

Ephemeral Ports: Why That Web Connection Uses Port 52744

TCP: Putting the Missing Bytes Back in Order

QUOTE:
"Amateurs hack systems, professionals hack people."
