LightAtom

What Is a Socket?

A socket is the endpoint an app uses for network communication. Learn how sockets relate to IP addresses and ports, and how to see them on your Mac.

A socket is an endpoint for network communication inside a program: a combination of an IP address, a port number and a protocol (TCP or UDP). Apps create sockets to listen for connections or to connect to servers, and the operating system moves data between them.

How it works

Safari's socket at 192.168.1.23:52344 connected over TCP to the web server's socket at 17.253.144.10:443.

The socket API (from BSD Unix, which macOS inherits) gives programs a handful of operations:

  • A server creates a socket, binds it to an address and port (say *:443), and listens. When a client connects, it accepts a new socket for that connection.
  • A client creates a socket and connects to the server's address and port. The OS picks a temporary local port for it.
  • Both sides then send and receive data, and close when done.

A TCP connection is uniquely identified by its two sockets: local IP and port, remote IP and port. A busy web server has one listening socket and thousands of connected sockets, all on port 443, each with a different client address and port.

UDP sockets don't connect; one socket can send to and receive from many addresses. That's why a single UDP socket (like Chrome's QUIC socket) can talk to many servers.

Socket vs port

A port is just a 16-bit number. A socket is the full endpoint inside a program, and its state: listening, established, closing. Many sockets can share one port.

Examples

lsof -nP -i lists sockets with their owning processes:

ControlCe 499 jane  10u IPv4 … TCP *:5000 (LISTEN)
Safari   1201 jane  28u IPv4 … TCP 192.168.1.23:60330->17.253.144.10:443 (ESTABLISHED)
mDNSResp  204 _mdns  8u IPv6 … UDP *:5353

ControlCenter has a listening TCP socket on port 5000, Safari a connected socket to Apple's server, and mDNSResponder a UDP socket for Bonjour.

On a Mac

  • lsof -nP -i shows sockets per process (add sudo for system processes).
  • netstat -an lists all sockets and their states, without process names.
  • nettop shows live traffic per socket and process.

Activity Monitor's Network tab counts bytes per process, which is the sum of that process's sockets.

See it in the app

LightAtom is built on socket data: its connections view shows every socket of every process, grouped by app, with bytes per connection, and its Ports view the listening ones.

Frequently asked questions

What is a socket in simple terms?

The connection point a program uses to talk over the network, like a phone line's handset. It combines an IP address, a port and a protocol.

What is the difference between a socket and a port?

A port is a number that identifies a service. A socket is a live endpoint in a program, with an address, port, protocol and state. Many sockets can use the same port.

What is a listening socket?

A socket a server has bound to a port and is waiting on for incoming connections. lsof marks it (LISTEN).

What is a WebSocket?

A protocol for a long-lived, two-way connection between a browser and a server, built on top of a normal TCP socket. Despite the name, it's an application protocol, not an OS socket.

LightAtom team

The people who build LightAtom, a network monitor for macOS.