How do Java's Socket and ServerSocket handle blocking TCP, from accept to half-close and timeouts, and why did that lead to thread-per-connection?
answer
- bind, listen, accept, repeat
- one stream pair per socket
- half-close keeps the reply path
- timeouts on connect, read and accept
- close from another thread unblocks
basics
~20 sServerSocket.accept() blocks until a client connects and returns a Socket with one input and one output stream. Its reads block too unless setSoTimeout sets a limit, and one thread cannot wait on several Sockets, so each connection got its own thread.
solid answer
~50 sA server binds a `ServerSocket` (backlog 50 by default) and loops on `accept()`, which blocks until a client connects and returns a connected `Socket`. Each `Socket` has an `InputStream` and an `OutputStream`; closing either closes the socket, so a client that has finished sending calls `shutdownOutput()` instead: the peer's `read()` sees `-1` while the reply still flows back. Timeouts are opt-in: `connect(address, millis)` for connecting, `setSoTimeout(millis)` for `read()` and `accept()`, which then throw `SocketTimeoutException` but leave the socket usable; `SO_TIMEOUT` does not cover `write()`. To free a blocked thread, close the socket from another thread; on a platform thread an interrupt does not wake a plain `Socket` read. Because every call holds its thread and a `Socket` cannot wait on several connections, servers ran a thread per connection, the cost that NIO selectors and later virtual threads relieved.
code
java · 26 linesimport java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class EchoServer {
public static void main(String[] args) throws IOException {
try (ServerSocket server = new ServerSocket(9000, 50);
ExecutorService perConnection = Executors.newVirtualThreadPerTaskExecutor()) {
while (true) {
Socket socket = server.accept(); // blocks until a client connects
perConnection.submit(() -> handle(socket));
}
}
}
static void handle(Socket socket) {
try (socket) {
socket.setSoTimeout(30_000); // each read() waits at most 30 s
socket.getInputStream().transferTo(socket.getOutputStream());
} catch (IOException e) { // includes SocketTimeoutException
System.err.println("connection ended: " + e);
}
}
}go deeper
Recall the server loop: bind a ServerSocket, accept() a Socket, read and write its two streams, and close it when the conversation ends.
Explain half-close with shutdownOutput(), which timeouts exist for connect, read and accept, and why closing a wrapped stream ends the whole connection.
Show how you free threads stuck in blocking calls, combine per-call timeouts with an overall deadline, and why an interrupt behaves differently on platform and virtual threads.
Judge when plain thread-per-connection code on virtual threads is enough and when a team still needs an event-loop design, weighing operability, memory and the libraries in use.
## Two classes, one blocking model `java.net.ServerSocket` listens for TCP connections and `java.net.Socket` is one end of a connection; both date from JDK 1.0. Every operation that waits (accepting, connecting, reading, writing) blocks the calling thread until it completes, fails or times out. Since JDK 13 (JEP 353) both are implemented on the internal `sun.nio.ch.NioSocketImpl`, which shares NIO's infrastructure; the specified behaviour stayed the same, though JEP 353 lists a few corner cases of unspecified behaviour that changed. ## The server side 1. `new ServerSocket(port, backlog)` creates the socket, binds it and starts listening; `new ServerSocket()` followed by `bind(address, backlog)` does the same in two steps. Port `0` asks for an ephemeral port, which `getLocalPort()` reports. 2. `accept()` blocks until a client connects and returns a new, connected `Socket` for that client, while the `ServerSocket` keeps listening. 3. The server hands that `Socket` to whatever serves the connection and goes back to `accept()`. 4. `close()` stops listening, and a thread blocked in `accept()` gets a `SocketException`. The order matters: `accept()` on a server socket that was never bound throws `SocketException`. The **backlog** is the requested maximum number of pending connections; without one the default is 50, a value of 0 or less selects an implementation-specific default (50 in the JDK), and the Javadoc says its exact semantics are implementation specific. ## A connection: two streams and half-close - `getInputStream()` and `getOutputStream()` give the two directions of the connection, and only once the socket is connected. - A `read()` may return fewer bytes than asked for and returns `-1` at end of stream, so framing messages is the application's job. - **Closing either stream closes the whole socket.** Wrapping the output stream in a writer and closing the writer therefore ends the connection. - `shutdownOutput()` is the **half-close**: later writes on this side throw, while the peer's `read()` returns `-1` once it has read everything sent before. The other direction stays open, so a client can send a request, half-close, then read the reply. - `shutdownInput()` makes later reads on this side return `-1`, and `close()` closes the socket and both streams. ## Timeouts | Operation | How you bound it | When the time runs out | |---|---|---| | `new Socket(host, port)` | you cannot: it connects inside the constructor | the operating system's connect timeout applies | | `connect(address, millis)` | `millis`; `0` sets no Java-side limit, so only the operating system's connect timeout applies | `SocketTimeoutException`, and the socket is closed | | `read()` on the input stream | `setSoTimeout(millis)`, set before the read | `SocketTimeoutException`; the socket stays usable | | `ServerSocket.accept()` | `ServerSocket.setSoTimeout(millis)` | `SocketTimeoutException`; the server stays usable | | `write()` on the output stream | `SO_TIMEOUT` does not apply | the call can block indefinitely | - `SO_TIMEOUT` bounds each blocking call, not a whole exchange: a peer that sends one byte every few seconds never trips it, so an overall deadline is yours to enforce. - The connect timeout does not cover name resolution. `new InetSocketAddress(host, port)` resolves the name before `connect()` starts. ## Unblocking a blocked thread - `close()` from another thread is the reliable way out: the blocked `read()`, `write()` or `accept()` throws `SocketException`. - An interrupt depends on the thread. `Socket`'s Javadoc makes its blocking methods interruptible only when the socket belongs to a `SocketChannel` or the caller is a **virtual thread**. On a virtual thread the interrupt wakes it, closes the socket and throws `SocketException` with the interrupted status set. A platform thread blocked in a plain `Socket` read stays blocked; only its interrupted status is set. ## Why this shape meant one thread per connection - Each blocking call ties up the calling thread, and a `Socket` cannot wait on several connections at once: it cannot be registered with a `Selector`. - A server therefore ran an accept loop plus one thread per connection, usually from a pool. A pool only caps the cost: with platform threads each connection holds an OS thread, and JEP 444 notes that the number of threads often becomes the limit long before CPU or network connections run out. - The two ways out were non-blocking channels with selectors, covered with NIO, and, since JDK 21, **virtual threads**: blocking calls on `Socket` and `ServerSocket` release the underlying platform thread while they wait. Inside `NioSocketImpl`, timed operations switch the socket to non-blocking mode and poll, and on a virtual thread its private `park` method hands the wait to the JDK's poller instead of blocking the carrier.
- Why does closing the stream from getOutputStream() break a request-and-reply client, and what should it call instead?Closing either of a socket's streams closes the socket itself, so the reply can no longer be read. Call `shutdownOutput()` instead: the peer's `read()` returns `-1` once it has consumed the request, and the input side stays open for the reply. The same trap hides in closing a writer or a buffered stream that wraps the socket's output stream.
- A platform thread is blocked in read() on a Socket made with new Socket(host, port). How do you stop it?Interrupting it does not wake it; the Javadoc makes such reads interruptible only on a virtual thread or for a channel's socket. Close the socket from another thread, and the read throws `SocketException`. To stop at a known moment instead, set `setSoTimeout()` before reading and check a flag each time `SocketTimeoutException` fires.
- Does setSoTimeout() bound the total time to receive a whole response?No. It bounds each blocking `read()` call. A peer that sends a byte every few seconds keeps every call under the limit while the whole response takes minutes, so an overall deadline needs its own clock, for example a scheduled task that closes the socket when it expires.
- setSoTimeout() also limits how long a write() can block.
- Interrupting a platform thread wakes it from a blocked read on a Socket built by its constructor.
- Closing a socket's OutputStream half-closes the connection and keeps the input open.
- A SocketTimeoutException from read() means the socket is broken and must be closed.
- Calling setSoTimeout() before connect() limits how long connecting takes.
- java.net.Socket and ServerSocket
- Java 1.0
- Socket and ServerSocket setSoTimeout (read, accept)
- Java 1.1
- Socket.shutdownInput and shutdownOutput (half-close)
- Java 1.3
- Socket.connect with a timeout
- Java 1.4
- Virtual threads
- Java 21pre-release 19
Assumes JDK 27; Socket and ServerSocket have run on NioSocketImpl since JDK 13, and their virtual-thread interrupt behaviour was specified in JDK 19 with the virtual-threads preview and is final since JDK 21.
context
- Socket (Java SE 27 & JDK 27)docs.oracle.com
- ServerSocket (Java SE 27 & JDK 27)docs.oracle.com
- JEP 444: Virtual Threadsopenjdk.org