~/ learn/ comp-348/ cards/ Socket options and the exception family
1 of 6

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

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/5880897b-76ae-49d6-8bc3-a11453b1b655/flashcard utf-8 LF