Chapter 25 of 28

Ports & Sockets

When your computer is connected to a network, it may be running many applications at the same time.

For example, you might be:

  • Browsing a website

  • Downloading a file

  • Using a messaging app

  • Playing an online game

So, when network data arrives at your computer, how does the operating system know which application should receive it?

This is where ports and sockets come in.

In simple terms:

IP address identifies the host, while a port number identifies a network service or endpoint on that host. A socket represents an endpoint used by an application for network communication.


What Is a Port?

A port number is a logical number used by the transport layer to identify a particular service or application endpoint on a device.

TCP and UDP use 16-bit port numbers, so the range is:

0 to 65,535

For example:

192.168.1.10 : 443
       ↑         ↑
    IP Address  Port

Here:

  • 192.168.1.10 identifies the host

  • 443 identifies the transport-layer port

This allows a single device to run many network services simultaneously.


Why Do We Need Ports?

An IP address can identify a device, but it does not tell us which application should receive the data.

Imagine your computer has:

IP Address: 192.168.1.10

At the same time, it may be running:

Web Server       → Port 443
DNS Service      → Port 53
Other Application→ Another Port

When network traffic arrives, the operating system uses the transport-layer information, including the destination port, to deliver the data to the appropriate application or socket.


Port Number Ranges

Port numbers are commonly divided into three ranges.

Range

Name

Common Use

0–1023

Well-known ports

Common standard services

1024–49151

Registered ports

Applications and services

49152–65535

Dynamic/private ports

Often used for temporary client-side connections

The exact use of a port depends on the application and operating system.


Common Port Numbers

Some port numbers are widely associated with particular services.

Port

Protocol

Common Service

20/21

TCP

FTP

22

TCP

SSH

25

TCP

SMTP

53

UDP/TCP

DNS

67/68

UDP

DHCP

80

TCP

HTTP

123

UDP

NTP

443

TCP

HTTPS

These are common conventions, not absolute rules. A service can sometimes be configured to use a different port.


What Is a Socket?

A socket is a software endpoint used by an application to communicate over a network.

A socket is associated with transport information such as an IP address, port, and protocol.

For example:

IP Address + Port + Protocol
192.168.1.10 : 5000 : TCP

This identifies a TCP communication endpoint.

Sockets are provided through the operating system's networking interface, allowing applications to send and receive data without having to directly manage the underlying network hardware.


Socket Example

Suppose a web server is running on:

IP Address: 192.168.1.20
Port: 443
Protocol: TCP

A client might connect using:

Client:
192.168.1.10 : 52000

Server:
192.168.1.20 : 443

The client uses a temporary port, while the server listens on its service port.

The communication can be represented as:

192.168.1.10:52000
        │
        │ TCP Connection
        ↓
192.168.1.20:443

Socket Pair

For a TCP connection, the communication can be identified by the combination of:

  • Source IP address

  • Source port

  • Destination IP address

  • Destination port

  • Transport protocol

For example:

Client:
192.168.1.10 : 52000

Server:
203.0.113.20 : 443

Together with TCP, these values identify the particular communication flow.

This is why a single server can communicate with many clients at the same time.

Client A ──┐
Client B ──┼──→ Server : 443
Client C ──┤
Client D ──┘

Each connection has different client-side addressing information, allowing the server to distinguish between them.


Ports in TCP and UDP

TCP and UDP have separate port spaces.

For example, TCP port 53 and UDP port 53 are different transport endpoints.

TCP : 53
UDP : 53

They do not represent the same transport connection simply because the numerical port is the same.


Client Port and Server Port

In many client-server applications, the server listens on a known port while the client uses a temporary ephemeral port.

For example:

Client                         Server

10.0.0.5:49152  ───────────→  10.0.0.10:443
       Ephemeral Port             Server Port

The server listens on port 443, while the client might use an automatically selected temporary port.

This allows the client to maintain multiple connections simultaneously.


Ports and Firewalls

Ports are also important for network security.

A firewall can use port information when deciding whether to allow or block traffic.

For example, an administrator might configure a firewall to allow HTTPS traffic to a server while blocking unwanted incoming connections.

Internet
   ↓
Firewall
   ↓
Allow TCP 443
   ↓
Web Server

However, a port number by itself does not guarantee that a service is secure. Security also depends on authentication, encryption, software configuration, and other controls.


IP Address vs Port vs Socket

These three concepts can be confusing, so remember this:

Concept

What It Identifies

IP Address

A host/interface at the network layer

Port

A transport-layer service or endpoint

Socket

A software communication endpoint used by an application

Think of a simple analogy:

IP Address → Building address

Port → Room number

Socket → The communication endpoint in that room

The analogy is not technically exact, but it makes the basic idea easy to remember.


Simple Real-Life Example

Suppose you open a website.

Your browser may create a connection like:

Your Computer
192.168.1.10:52000
       │
       │ TCP
       ↓
Web Server
142.250.x.x:443

Here:

  • Your computer's IP identifies your host.

  • 52000 is a temporary client port.

  • The server's IP identifies the destination host.

  • 443 identifies the HTTPS service.

  • TCP provides the transport connection.

The operating system uses this information to deliver network data to the correct application.

Conclusion

Ports and sockets help operating systems and applications manage network communication.

A port is a transport-layer number used to identify an application or service endpoint, while a socket is a software endpoint through which an application communicates over a network.

The easiest way to remember the relationship is:

IP Address → Which host?

Port → Which service/application endpoint?

Socket → Communication endpoint used by the application

Once you understand ports and sockets, concepts such as client-server communication, TCP connections, firewalls, DNS, HTTP/HTTPS, and network programming become much easier to understand.