The Transport Layer

a socket

Think of a socket as the doorway between a program and the network — the place where bytes enter and leave. Just as a wall socket is the standard outlet you plug an appliance into without caring how the power plant works, a network socket is the standard outlet a program plugs into to send and receive data without caring how routers and links do their job. To the program, it usually looks like a thing you can write bytes into and read bytes out of.

More precisely, a socket is the endpoint of a communication, identified to the network by an (IP address, port number) pair, and managed by the operating system. The transport layer uses sockets as the targets of demultiplexing: when a segment arrives, the OS looks at the addressing fields and decides which socket — and therefore which program — should receive it. A TCP socket represents one reliable byte-stream connection and is keyed by the full four-tuple (source IP and port, destination IP and port); a UDP socket is keyed just by its local (IP, port) and can exchange datagrams with many peers.

Why it matters: the socket is the boundary where the clean ideas of this field meet real programs. Everything above (an application's messages) and everything below (multiplexing, ports, reliability) connects through it. Note the distinction this field cares about: here a socket is the abstract transport endpoint, the thing that gets demultiplexed to. The detailed programming interface for creating, binding, and using sockets — the Berkeley socket API — is a separate topic; here we care about what a socket is, not the function calls used to operate one.

A web server creates a listening TCP socket bound to (its IP, port 443). For every client that connects, the OS spins up a separate connected socket keyed by that client's full four-tuple, so a thousand visitors are a thousand independent sockets sharing one port.

One listening socket, many connected sockets — all on port 443.

Don't confuse the transport-layer socket (an addressing endpoint) with the programming API. Also, a 'socket' in everyday talk often means the whole (IP, port) pair; strictly, that pair identifies the socket, it isn't a second meaning.

Also called
network socket插座通訊端點