Options that need setting before the socket connects, then the connect with its own deadline.
Options that need setting before the socket connects, then the connect with its own deadline.
Answer
try (Socket socket = new Socket()) { socket.setSoTimeout(10_000); socket.setTcpNoDelay(true); socket.setKeepAlive(true); socket.connect(new InetSocketAddress(host, port), 5_000); // ... speak the protocol ... }
The no-argument constructor is what makes this ordering possible. Every setter here throws `SocketException` if the platform cannot honour the option, so they belong inside the same try as the connect.
Harold 4e ch8 §Setting Socket Options; §Socket Exceptions