Which ephemeral port has java InetSocketAddress associated with?

What I would like to achieve

Associates a server with an ephemeral port for unit testing purposes.

My problem:

Using JDK 1.5.0_22, I am trying to associate InetSocketAddress with an ephemeral port using port 0 according to javadoc, but I cannot find the path from the address object to find out which port it is bound to, so I cannot configure my clients as follows :

InetSocketAddress address = new InetSocketAddress(0);
assertThat(address.isUnresolved(), is(false));
assertThat(address.getPort(), is(0));

I cannot correctly understand the javadoc suggestion:

The valid port value is from 0 to 65535. A port number equal to zero will let the system pick up the ephemeral port in the bind operation.

, ( , ) , 0 ( http://simpleweb.sourceforge.net/):

    Container httpServer = new Container() {

        public void handle(Request req, Response resp) {
        }
    };
    SocketConnection connection = new SocketConnection(httpServer);
    InetSocketAddress address = new InetSocketAddress(0);
    connection.connect(address);

    assertThat(address.isUnresolved(), is(false));
    assertThat(address.getPort(), is(0));

nmap, , , . ?

+5
1

InetSocketAddress, 0, connect(), , . connection.getLocalPort() ((InetSocketAddress)connection.getLocalSocketAddress()).getPort() , .

+10

All Articles