Home/Learn/Computer Networks/Ports & Sockets

Ports & Sockets

Beginner
Transport Layer

A port identifies a specific process on a host; a socket is the combination of IP address + port + protocol that uniquely identifies one end of a network connection.

Overview

IP addresses route packets to the correct machine; ports route them to the correct process on that machine. A port is a 16-bit number (0–65535). Well-known ports (0–1023) are reserved — HTTP is 80, HTTPS is 443, SSH is 22, DNS is 53. Registered ports (1024–49151) are assigned by IANA to specific applications. Ephemeral (dynamic) ports (49152–65535) are assigned by the OS to client-side connections. A socket is the concrete communication endpoint: (protocol, local IP, local port, remote IP, remote port). This 5-tuple uniquely identifies every active connection. A server process binds to a port; the OS multiplexes many client connections on the same server port by tracking the full 5-tuple for each. Understanding ports and sockets is fundamental to configuring firewalls, load balancers, and diagnosing connection errors.

Port Ranges and Well-Known Ports

Port ranges have standardised meanings. Binding to ports below 1024 requires root/admin privileges on Unix systems. Clients use ephemeral ports chosen by the OS.

Well-known ports and Java socket ports
// Well-known ports (0–1023):
// 20/21  — FTP (data/control)
// 22     — SSH
// 25     — SMTP
// 53     — DNS (UDP + TCP)
// 80     — HTTP
// 110    — POP3
// 143    — IMAP
// 443    — HTTPS (HTTP over TLS)
// 3306   — MySQL
// 5432   — PostgreSQL
// 6379   — Redis
// 8080   — HTTP alternate (dev servers, Spring Boot default)
// 9092   — Apache Kafka

// Java: bind server to a specific port
ServerSocket server = new ServerSocket(8080);   // privileged port: needs root if < 1024
System.out.println("Listening on port 8080");

Socket client = new Socket("api.example.com", 443);
int localPort = client.getLocalPort();          // OS assigned ephemeral port, e.g. 54231
int remotePort = client.getPort();              // 443
System.out.println("Ephemeral port: " + localPort);

Socket 5-Tuple and Multiplexing

A single server port (e.g., 443) handles thousands of simultaneous client connections because each connection is uniquely identified by the 5-tuple: protocol + server IP + server port + client IP + client port. The OS dispatches incoming packets to the right socket using this 5-tuple.

5-tuple multiplexing and Java NIO selector
// How one server port handles many clients:
//
// Connection 1: TCP, 10.0.0.1:443 ↔ 203.0.113.10:54231
// Connection 2: TCP, 10.0.0.1:443 ↔ 203.0.113.20:61002
// Connection 3: TCP, 10.0.0.1:443 ↔ 198.51.100.5:49812
//
// All three arrive at port 443, but the OS uses the full 5-tuple
// to dispatch each packet to the correct socket/thread.

// Java NIO — non-blocking server that handles many clients on one thread:
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.bind(new InetSocketAddress(8080));
serverChannel.configureBlocking(false);

Selector selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);

while (true) {
    selector.select();                          // blocks until a channel is ready
    for (SelectionKey key : selector.selectedKeys()) {
        if (key.isAcceptable()) {
            SocketChannel client = serverChannel.accept();
            client.configureBlocking(false);
            client.register(selector, SelectionKey.OP_READ);
        } else if (key.isReadable()) {
            // handle read on this client socket
        }
    }
}

Key Points to Remember

  • 1Ports (0–65535) identify processes; IP addresses identify hosts.
  • 2Well-known ports: HTTP=80, HTTPS=443, SSH=22, DNS=53, MySQL=3306, Redis=6379.
  • 3Ports below 1024 require root privileges to bind on Linux/Mac.
  • 4A socket is the 5-tuple: (protocol, src IP, src port, dst IP, dst port).
  • 5Ephemeral ports (49152–65535) are OS-assigned to the client side of connections.
  • 6One server port can handle thousands of connections — differentiated by client IP+port.

Interview Questions

Sign in to ask Aria
1

What is the difference between a port and a socket?

EasyWipro
2

How can a server listen on port 443 and handle thousands of simultaneous connections?

MediumAmazon
3

What are ephemeral ports and when does the OS assign them?

EasyPersistent
4

Why do ports below 1024 require root privileges on Linux?

EasyThoughtWorks
5

Explain how Java NIO selectors enable handling many connections with a single thread.

HardFlipkart

Ask Aria about Ports & Sockets

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…