JDK-6938596 : Undocumented requirement : explicit bind with constructor Socket(String,int)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u18
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2010-03-26
  • Updated: 2010-05-06
  • Resolved: 2010-05-06
Related Reports
Relates :  
Description
OPERATING SYSTEM
Windows

FULL JDK VERSION
Java 6

PROBLEM DESCRIPTION
Due to intentional changes to the constructor Socket(String, int) under CR 6452458 (6u2), an explicit bind is necessary once the Socket is created.

We believe that this should be explained in the API documention. If the bind is not invoked explicitly (i.e. the user assumed that the constructor was doing it for them, as it did for all Java versions between 1.4.x and 6u2) then the same port number can be returned for two socket connections, leading to incorrect behaviour.

Comments
EVALUATION An explicit bind is not required after calling the Socket(String,int) constructor. In fact it, invoking bind on the returned socket will cause a java.net.SocketException "Already bound" to be thrown. Example: public class Connected { public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: java Connected <host> <port>"); return; } Socket socket = new Socket(args[0], Integer.parseInt(args[1])); System.out.println(socket.isBound()); System.out.println(socket.getLocalSocketAddress()); socket.bind(new InetSocketAddress(0)); } } Removing the explicit bind from the socket implementation, see CR 6452458, only forces the OS to do an implicit bind before connecting the socket. There is no issue with local ports being confused or reused by two (or more) sockets. The OS will ensure that each connected socket will have a unique local socket address.
07-04-2010