Code example for ServerSocket

Methods: close, getLocalPort

0
    private int allocatePort() 
    { 
        ServerSocket ss;
        try 
        { 
            ss = new ServerSocket( 0 );
        } 
        catch ( IOException e )
        { 
            return 0; 
        } 
        int port = ss.getLocalPort();
        try 
        { 
            ss.close();
        } 
        catch ( IOException e )
        { 
            // does it matter? 
            fail( "Error allocating port " + e.getMessage() );
        }