Zamil's CSE Directory

computer networking

Ports and Sockets: Connecting Network Traffic to Applications

Learn how computers use ports and sockets to route network data to the correct applications.

#networking#ports#sockets#tcp#udp
networking, ports, sockets, tcp, udp guides

In the previous chapter we explored TCP and UDP, the protocols responsible for transporting data between computers.

But a question still remains:

How does your computer know which application should receive incoming data?

Your device may be running many networked programs at the same time:

  • a web browser
  • a messaging app
  • a video call
  • a game client
  • background system services

All of these applications communicate over the network simultaneously. When packets arrive at your computer, the system must determine which program the data belongs to.

This problem is solved using ports and sockets.

Together, ports and sockets allow the operating system to route network traffic to the correct application.


Why Ports Exist

An IP address identifies a device on a network.

For example, a server might have an address like:


203.0.113.10

However, a single machine may host many different services.

For example, the same server might provide:

  • a website
  • an email service
  • a file transfer service
  • an API

If a packet arrives at the server, the system needs a way to determine which service should handle it.

This is the role of ports.

A port identifies a specific application or service running on a computer.

You can think of ports as numbered doors on a building.

  • The IP address identifies the building.
  • The port number identifies the specific door where the message should be delivered.

Port Numbers

Ports are identified by numbers ranging from 0 to 65535.

When an application communicates over the network, it sends or receives data through a specific port.

For example:


192.0.2.10:80

This means:

  • 192.0.2.10 is the IP address
  • 80 is the port number

Together they specify the destination of the communication.


Common Well-Known Ports

Some port numbers are standardized and commonly associated with specific services.

These are often called well-known ports.

Examples include:

PortService
80HTTP (web traffic)
443HTTPS (secure web traffic)
25SMTP (email sending)
53DNS
22SSH (secure remote login)

When your browser connects to a website, it usually communicates with port 80 or 443 on the server.

This allows the server to direct the request to its web service.


How Ports Work in Practice

Let’s imagine you open a website in your browser.

Your computer might send a request that looks like this:


Destination IP: 93.184.216.34
Destination Port: 443

This tells the destination server:

Deliver this data to the application listening on port 443.

The server’s web software receives the request and sends a response back.

Without ports, a server would not know which program should handle incoming data.


Client Ports and Server Ports

In most communications there are two ports involved:

  • a server port
  • a client port

The server listens on a well-known port.

For example:


Web server → port 443

Meanwhile, your computer chooses a temporary port number for the connection.

Example:


Your computer: 192.168.1.25:51734
Server: 93.184.216.34:443

This allows your computer to maintain multiple connections at the same time.

Each connection can be uniquely identified by:

  • source IP
  • source port
  • destination IP
  • destination port

Introducing Sockets

Ports identify where network traffic should go, but applications need a programming interface to actually send and receive data.

This interface is called a socket.

A socket is a software structure used by programs to communicate over the network.

It connects an application to the networking system provided by the operating system.

You can think of a socket as the endpoint of a network communication channel.


How Sockets Connect Applications

When a program wants to communicate over the network, it creates a socket.

A simplified interaction might look like this:

graph LR
  A[Application] --> B[Socket]
  B --> C[Operating System]
  C --> D[Network]

The application sends data to the socket, and the operating system handles:

  • packaging the data into packets
  • using TCP or UDP
  • sending packets across the network

Incoming packets follow the reverse path back to the application.


Server Sockets and Listening

Servers often create listening sockets.

A listening socket waits for incoming connections on a specific port.

For example:

sequenceDiagram
  participant Client
  participant Server

  Client->>Server: Connect to port 443
  Server->>Client: Accept connection

When a client connects, the server creates a new socket to handle that specific connection.

This allows a server to manage many clients simultaneously.


Putting It All Together

When two applications communicate across a network, several components work together:

  1. IP addresses identify the devices.
  2. Ports identify the specific applications.
  3. Sockets allow programs to send and receive data.

A typical connection might look like this:

Client: 192.168.1.25:51734
Server: 93.184.216.34:443
Protocol: TCP

This uniquely identifies the communication between two programs.


Key Ideas to Remember

Ports and sockets allow multiple applications to share a single network connection.

Important ideas from this chapter include:

  • An IP address identifies a device on a network.
  • A port identifies a specific service or application on that device.
  • Port numbers range from 0 to 65535.
  • Many common services use standard well-known ports.
  • A socket is the interface applications use to communicate over the network.
  • Servers listen for connections on specific ports while clients use temporary ports.

Together, these mechanisms ensure that network traffic reaches the correct application on the correct device.


What Comes Next

Now that we understand how applications connect using ports and sockets, we can explore how the web itself operates.

When you open a website, your browser communicates with a server using a protocol specifically designed for the web.

In the next chapter we will explore how the web works, including the protocols that power it:

HTTP and HTTPS.