是太简单了还是怎么样?都没人理的。
我就不知道怎么随机获得一个空闲端口啊。
用随机尝试建一下是否已经被占用,然后free掉吗?

解决方案 »

  1.   

    passive模式的话,用new ServerSocket(),然后bind,获取一个空闲端口。
      

  2.   

    不看文档的人……public ServerSocket(int port)
    Creates a server socket, bound to the specified port. A port of 0 creates a socket on any free port. 
    port设为0就自动拿一个空闲端口。
    或者bind(null)也行。
    public void bind(SocketAddress endpoint)
              throws IOExceptionBinds the ServerSocket to a specific address (IP address and port number). 
    If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.