-
Sub-task
-
Resolution: Delivered
-
P4
-
24
The `connect` methods defined by `java.net.Socket` are now specified, and changed, to close the `Socket` if the connection cannot be established, or the timeout (if provided) expires before the connection is established.
The behavior in previous JDK releases was to close the underlying socket when a connection could not be established but leave the `Socket` open. The resulting `Socket` was "useless" as most methods behaved as if the `Socket` was closed.
The behavior of the `connect` methods when invoked with an unresolved `InetSocketAddress` was unspecified in previous releases. The behavior in JDK 11 and older releases was to close the underlying socket, throw `UnknownHostException`, but leave the `Socket` open in a "useless" state. The behavior in JDK 12 to JDK 23 was to throw `UnknownHostException` without closing the `Socket` or underlying socket. This case is now specified, and changed, to close the `Socket`, the underlying socket, and throw `UnknownHostException` if the connection cannot be established because the address is unresolved.
As part of this change, calling the `connect` method on a `Socket` obtained from a `SocketChannel` will now throw `IOException` if already connected. This is the specified exception for this scenario. The incorrect behavior in previous releases was to throw `AlreadyConnectedException` for this case.
The behavior in previous JDK releases was to close the underlying socket when a connection could not be established but leave the `Socket` open. The resulting `Socket` was "useless" as most methods behaved as if the `Socket` was closed.
The behavior of the `connect` methods when invoked with an unresolved `InetSocketAddress` was unspecified in previous releases. The behavior in JDK 11 and older releases was to close the underlying socket, throw `UnknownHostException`, but leave the `Socket` open in a "useless" state. The behavior in JDK 12 to JDK 23 was to throw `UnknownHostException` without closing the `Socket` or underlying socket. This case is now specified, and changed, to close the `Socket`, the underlying socket, and throw `UnknownHostException` if the connection cannot be established because the address is unresolved.
As part of this change, calling the `connect` method on a `Socket` obtained from a `SocketChannel` will now throw `IOException` if already connected. This is the specified exception for this scenario. The incorrect behavior in previous releases was to throw `AlreadyConnectedException` for this case.