Sockets in Operating System

What is Socket?

Sumit Bopche
4 min readNov 20, 2022

A socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network. A socket is externally identified to other hosts by its socket address.

In the standard Internet protocols TCP and UDP, a socket address is the combination of an IP address and a port number. If we want to create multiple socket connections from the same host, we will be able to do so because OS will assign a different port number for each socket connection. That way we can have multiple connections between our host and the server.

Combining the transport layer port number and the network layer IP address uniquely identifies a particular application process running on an individual host device. This combination is called a socket.

IP Address:

An Internet Protocol address (IP address) is a numerical label such as 192.0.2.1 that is connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions: network interface identification and location addressing.

Port Number:

A port number is a 16-bit unsigned number assigned to uniquely identify a connection endpoint and to direct data to a specific service

The port numbers are divided into three ranges:

  • the well-known ports (0 to 1023) and these are reserved
  • the registered ports (1024 to 49151)
  • the dynamic or private ports (49152 to 65535)

Notable port numbers:


+----------+--------------------------------------------------------------------+
| Number | Assignment |
+----------+--------------------------------------------------------------------+
| 20 | File Transfer Protocol (FTP) Data Transfer |
| 21 | File Transfer Protocol (FTP) Command Control |
| 22 | Secure Shell (SSH) Secure Login |
| 23 | Telnet remote login service, unencrypted text messages |
| 25 | Simple Mail Transfer Protocol (SMTP) email delivery |
| 53 | Domain Name System (DNS) service |
| 67, 68 | Dynamic Host Configuration Protocol (DHCP) |
| 80 | Hypertext Transfer Protocol (HTTP) used in the World Wide Web |
| 110 | Post Office Protocol (POP3) |
| 119 | Network News Transfer Protocol (NNTP) |
| 123 | Network Time Protocol (NTP) |
| 143 | Internet Message Access Protocol (IMAP) Management of digital mail |
| 161 | Simple Network Management Protocol (SNMP) |
| 194 | Internet Relay Chat (IRC) |
| 443 | HTTP Secure (HTTPS) HTTP over TLS/SSL |
| 546, 547 | DHCPv6 IPv6 version of DHCP |
+----------+--------------------------------------------------------------------+

A socket is one endpoint of a two-way communication link between two programs running on the network.

Sockets are mainly used for communication in Client-Server architecture-based systems.

  • The server waits for incoming client requests by listening to a specified port. Once a request is received, the server accepts a connection from the client socket to complete the connection.
  • Servers implementing specific services such as telnet (used for remote login), FTP(used for file transfer), and HTTP listen to well-known ports.

Communication using Sockets

Here we have two client hosts and one web server.

  • Each and every socket process will be assigned the IP and port number.
  • OS will assign any arbitrary number other than the reserved port numbers to these processes.
  • If two processes from the same host make socket connections with the server, both will be assigned different port numbers. Like in our case, process 1 is assigned 1786 port, and process 2 is assigned 1643.
  • The packets traveling between the hosts are delivered to the appropriate process based on the destination port number.

TCP / HTTP Listening On Ports: How Can Many Users Share the Same Port

As in our example, multiple clients are connecting on the same port number which is 80. So how can many users share the same port?

If the client connects to port 80, then his port must be 80 too? This is a sensible thing to think, but actually not what happens. If that were to be correct, we could only serve one user per foreign IP address. Once a remote computer connects, then he would hog the port 80 to port 80 connection, and no one else could connect.

Three things must be understood:

1.) On a server, a process is listening on a port. Once it gets a connection, it hands it off to another thread. The communication never hogs the listening port.

2.) Connections are uniquely identified by the OS by the following 5-tuple: (local-IP, local-port, remote-IP, remote-port, and protocol). If any element in the tuple is different, then this is a completely independent connection.

3.) When a client connects to a server, it picks a random, unused high-order source port. This way, a single client can have up to ~64k connections to the server for the same destination port.

References:

--

--