Networking Fundamentals
Networking Fundamentals
Every Java networking API — from raw Socket to the modern HttpClient — sits on top of the same foundational concepts: the TCP/IP protocol suite, ports, and the client/server model. Before touching a single line of networking code you must understand what happens at each layer, because those mental models determine how you design connections, handle errors, and tune performance in production systems.
The TCP/IP Protocol Suite
TCP/IP is not a single protocol; it is a four-layer stack. From lowest to highest:
- Network Access (Link) Layer — physical transmission: Ethernet frames, Wi-Fi packets. Java never touches this directly.
- Internet (IP) Layer — routes packets across networks using IP addresses. Each packet travels independently and may arrive out of order or not at all. IP is best-effort.
- Transport Layer — delivers data to the correct process. Two protocols live here: TCP and UDP.
- Application Layer — protocols your application implements on top of transport: HTTP/2, TLS, WebSocket, SMTP, and so on.
TCP: Reliable, Ordered, Connection-Oriented
TCP (Transmission Control Protocol) adds three guarantees on top of raw IP:
- Reliable delivery — lost packets are automatically retransmitted.
- Ordered delivery — bytes arrive in exactly the order they were sent.
- Flow & congestion control — the sender is throttled to match what the receiver and network can handle.
These guarantees come from the three-way handshake that establishes a connection before any application data flows:
Every Socket you create in Java triggers this handshake. Every socket.close() triggers the four-way close. These round trips have latency cost — that is why HTTP/1.1 introduced keep-alive and HTTP/2 introduced multiplexing: to reuse connections rather than rebuilding them for each request.
Ports and the Well-Known Port Assignments
The 16-bit port number ranges (0–65535) are partitioned by convention:
- 0–1023: Well-known ports — assigned by IANA. HTTP = 80, HTTPS = 443, SSH = 22, SMTP = 25. Binding to these requires root / administrator privilege on most OSes.
- 1024–49151: Registered ports — used by well-known application protocols but do not require elevated privilege. MySQL = 3306, PostgreSQL = 5432, Redis = 6379.
- 49152–65535: Dynamic / ephemeral ports — the OS assigns these automatically to the client side of a connection. When your Java client connects to a server, the OS picks an ephemeral port as the source port so the server can send replies back to that specific socket.
The Client/Server Model
All TCP-based Java networking follows the same structural split:
- Server — binds to a specific port on a specific interface, enters a listening state, and accepts incoming connection requests. It is passive.
- Client — knows the server's IP address and port, initiates the connection actively, and communicates once the handshake is complete.
In Java, ServerSocket represents the listening endpoint; each call to serverSocket.accept() blocks until a client connects and then returns a regular Socket representing that specific connection. The server typically hands that socket to a thread (or virtual thread in Java 21+) so it can accept the next client immediately.
IP Addresses: IPv4 and IPv6
Java's InetAddress class abstracts both address families:
InetAddress.getByName() performs a DNS lookup on the calling thread. In high-throughput servers this stalls threads. The modern HttpClient handles this asynchronously internally; with raw sockets you must off-load DNS lookups or use an async DNS library.
Sockets are I/O Resources — Always Close Them
Both Socket and ServerSocket implement AutoCloseable. Always use try-with-resources. An unclosed socket holds an OS file descriptor and a TCP connection slot; leaking sockets under load will exhaust both.
TCP Versus UDP: When Does the Reliability Tax Matter?
TCP's reliability guarantees are not free: retransmission adds latency, and head-of-line blocking means a lost packet stalls all subsequent data on that connection. UDP trades reliability for speed:
- No connection setup — send the first packet immediately.
- No retransmission — lost datagrams are gone.
- No ordering — packets may arrive in any sequence.
Use UDP when timeliness beats correctness: live video/audio streaming, online game state updates, DNS queries, and QUIC (the transport under HTTP/3). For REST APIs, database connections, file transfer, and most enterprise workloads, TCP is the right default.
Summary
TCP/IP is a layered stack where IP routes packets and TCP makes delivery reliable and ordered at the cost of handshake latency. Ports bind a transport endpoint to a specific process. The client/server model splits networking into an active connector and a passive listener — mirrored exactly by Socket and ServerSocket in Java. These are not just theoretical concepts: every timeout you set, every connection pool you size, and every retry policy you design is a direct consequence of understanding how TCP works at this level.