100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Ports and Sockets

Understand port number ranges and how a socket, the pairing of an IP address and port, identifies a network endpoint.

Transport LayerBeginner8 min readJul 8, 2026
Analogies

Introduction

Ports and sockets are the mechanism the transport layer uses to identify exactly which application process on a host should send or receive a given piece of data. Understanding the different port ranges and what a socket represents is essential for working with any network application.

🏏

Cricket analogy: Just as a stadium assigns specific gate numbers so ushers know exactly which section a ticket holder should enter, port numbers tell a host's operating system exactly which application process a piece of data belongs to.

Explanation

A port number is a 16-bit value (0-65535) that identifies a specific process on a host. Port numbers are divided into three ranges. Well-known ports (0-1023) are reserved for standard, widely used services and are typically assigned by the IANA - examples include port 80 for HTTP, port 443 for HTTPS, port 22 for SSH, and port 53 for DNS. Registered ports (1024-49151) are reserved for specific applications registered with IANA but are not as tightly controlled as well-known ports. Dynamic or ephemeral ports (49152-65535) are not assigned to any specific service; operating systems allocate them temporarily to client applications as the source port for outgoing connections.

🏏

Cricket analogy: Test cricket has fixed, globally recognized rules like well-known ports 80 and 443, domestic T20 leagues have registered formats specific to boards, and a pickup gully-cricket game assigns whatever rules the kids agree on that day, like an ephemeral port.

A socket is the combination of an IP address and a port number - for example, 192.0.2.10:443 - and it uniquely identifies one endpoint of a network communication. A TCP connection is actually identified by a pair of sockets: the source socket (client IP + ephemeral port) and the destination socket (server IP + well-known port). This is how a server can handle many simultaneous connections on the same well-known port (e.g., port 443) from many different clients: each connection is distinguished by the unique combination of both sockets, even though the destination socket is identical across all of them.

🏏

Cricket analogy: A specific fielding position, like gully at Eden Gardens, identifies one spot on the field, but a catch is really defined by the pair of positions, the bowler's end and the batsman's end, together, just like a TCP connection needs both sockets.

Example

python
import socket

# The server binds to a well-known port to listen for connections.
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 443))   # well-known port for HTTPS
server.listen(5)

conn, client_addr = server.accept()
# client_addr is something like ('203.0.113.7', 51422)
# 51422 is an ephemeral port the OS assigned to the client automatically.
print(f"Connection identified by sockets: server=(0.0.0.0,443) client={client_addr}")

Analysis

Notice that the server explicitly binds to port 443, a well-known port, because clients need to know in advance where to find the HTTPS service. The client, on the other hand, never explicitly chooses its port - the operating system automatically assigns an ephemeral port (here, 51422, safely within the 49152-65535 dynamic range) for the outgoing connection. Every incoming connection to the server will share the same destination socket (server_ip:443), but each will have a different source socket (a different client IP and/or ephemeral port), which is exactly how the server's TCP stack keeps thousands of simultaneous connections distinct from one another.

🏏

Cricket analogy: The stadium's main entry gate, well-known like port 443, is fixed and publicized in advance, but each spectator is auto-assigned a random seat number by the ticketing system, and the turnstile tracks thousands of fans simultaneously by their unique seat, not the shared gate.

Key Takeaways

  • Well-known ports (0-1023) are reserved for standard services, e.g., 80 (HTTP), 443 (HTTPS), 22 (SSH), 53 (DNS).
  • Registered ports (1024-49151) are assigned to specific applications but with looser control than well-known ports.
  • Dynamic/ephemeral ports (49152-65535) are temporarily assigned by the OS for outgoing client connections.
  • A socket is the pairing of an IP address and a port number, uniquely identifying a communication endpoint.
  • A TCP connection is identified by the combination of source socket and destination socket, allowing a server to handle many clients on one well-known port.

Practice what you learned

Was this page helpful?

Topics covered

#Python#ComputerNetworksStudyNotes#ComputerNetworks#PortsAndSockets#Ports#Sockets#Explanation#Example#Networking#StudyNotes#SkillVeris