sajad torkamani

A TCP socket is an endpoint for a single process on a host. This endpoint is identified by a combination of IP address and port number ( e.g., 172.1.167.13:5555). This combination of IP address and port number is used in the TCP/IP suite (at the network and transport layer) to route data from a source process to the target process.

TCP IP sockets

As an example, here’s how sockets are used in a HTTP request:

  • John opens a tab in a web browser to navigate to google.com. The browser creates a new process for that tab and assigns the process a unique socket ( e.g., 11.111.11.111:5555). This tab is now reachable at this socket.
  • The browser does some work to resolve the socket of the remote process listening at google.com ( e.g., 22.222.22.222:80) and issues a HTTP request.
  • During the data encapsulation stages of the TCP/IP protocols, the tab’s socket (11.111.11.111:5555) is included as part of the sending packet.
  • The TCP/IP suite uses the destination socket to route the request to the correct IP (22.222.22.222) and then to the correct port (80).
  • The web server listening on port 80 receives the incoming request and does some work to prepare a response.
  • The web server adds some metadata to the response that includes the original client socket.
  • The TCP/IP suite uses the destination socket (IP address and port number) to route the data to the original client process (browser tab).
  • The browser renders the response.

List TCP socket pairs

Linux (might need to run sudo apt-get install net-tools):

sudo netstat -npt

macOS:

netstat -n -p tcp

Example output:

Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)
tcp4       0      0  192.168.0.89.50679     17.253.35.209.80       ESTABLISHED
tcp4       0      0  192.168.0.89.50678     20.42.65.84.443        ESTABLISHED
tcp4       0      0  192.168.0.89.50674     52.109.32.7.443        ESTABLISHED
tcp4       0      0  192.168.0.89.50670     142.250.200.10.443     CLOSE_WAIT
tcp4       0      0  192.168.0.89.50660     104.18.22.110.443      ESTABLISHED
tcp4       0      0  192.168.0.89.50653     46.101.74.88.443       ESTABLISHED
tcp4       0      0  192.168.0.89.50311     209.97.138.40.22       ESTABLISHED
tcp4       0      0  192.168.0.89.50033     192.241.242.235.443    ESTABLISHED
tcp4       0      0  192.168.0.89.50019     66.102.1.188.443       ESTABLISHED
tcp4       0      0  192.168.0.89.50018     104.18.22.110.443      ESTABLISHED
tcp4       0      0  192.168.0.89.50016     142.250.187.234.443    ESTABLISHED
tcp4       0      0  192.168.0.89.50015     35.174.127.31.443      ESTABLISHED
tcp4       0      0  192.168.0.89.50014     17.57.146.5.5223       ESTABLISHED
tcp4       0      0  127.0.0.1.49189        127.0.0.1.49996        ESTABLISHED
tcp4       0      0  127.0.0.1.49996        127.0.0.1.49189        ESTABLISHED
tcp4       0      0  127.0.0.1.49240        127.0.0.1.49244        ESTABLISHED
tcp4       0      0  127.0.0.1.49244        127.0.0.1.49240        ESTABLISHED
Tagged: Networking