同上,谢谢

解决方案 »

  1.   

    最简单的办法是建议一个socket 连接,然后给这个连接绑上不同的端口上,来个for,
      

  2.   

    socketServer吧?socket我记得不能bind本地的固定端口.
      

  3.   

    public class Test2{
    public static void main(String[] args) {
    String host = "localhost";
    int host_count = 10;
    int j = 1;

    for (int i = 8010; i > 7990; i--) {
    if (!connect(host, i)) {
    int count = j++;
    if (count > host_count) {
    return;
    }
    System.out.println("空闲端口" + count + ":[" + i + "]");
    }
    }
    }

    public static boolean connect(String host, int port) {
    try {
    Socket socket = new Socket(host, port);
    boolean success = socket.isConnected();
    if (success) {
    socket.close();
    }
    return success;
    } catch (UnknownHostException e) {
    return false;
    } catch (IOException e) {
    return false;
    }
    }
    }