他的回答从来都不起作用。与Java7无关。这不会测试at端口是否可用。它测试它是否处于侦听状态,IP地址是否可访问,等等。此外,这个测试非常慢(每个端口将近一秒钟)。当捕获IOException时,它不应该返回false吗?@BaroudiSafwen它完

他的回答从来都不起作用。与Java7无关。这不会测试at端口是否可用。它测试它是否处于侦听状态,IP地址是否可访问,等等。此外,这个测试非常慢(每个端口将近一秒钟)。当捕获IOException时,它不应该返回false吗?@BaroudiSafwen它完,java,sockets,port,Java,Sockets,Port,他的回答从来都不起作用。与Java7无关。这不会测试at端口是否可用。它测试它是否处于侦听状态,IP地址是否可访问,等等。此外,这个测试非常慢(每个端口将近一秒钟)。当捕获IOException时,它不应该返回false吗?@BaroudiSafwen它完全取决于异常的实际情况。对于ConnectException:“连接被拒绝”,它应该返回false。对于超时,它可能返回的任何内容都无效,因为实际答案未知。这就是为什么这项技术在这方面是无用的。不是要求的。不是要求的。还有套接字泄漏。这适用于U


他的回答从来都不起作用。与Java7无关。这不会测试at端口是否可用。它测试它是否处于侦听状态,IP地址是否可访问,等等。此外,这个测试非常慢(每个端口将近一秒钟)。当捕获IOException时,它不应该返回false吗?@BaroudiSafwen它完全取决于异常的实际情况。对于
ConnectException:“连接被拒绝”
,它应该返回false。对于超时,它可能返回的任何内容都无效,因为实际答案未知。这就是为什么这项技术在这方面是无用的。不是要求的。不是要求的。还有套接字泄漏。这适用于UDP端口。问题似乎是关于TCP端口的。谢谢,在macOS 11(大苏尔)上为我工作过
public static boolean isAvailable(int portNr) {
  boolean portFree;
  try (var ignored = new ServerSocket(portNr)) {
      portFree = true;
  } catch (IOException e) {
      portFree = false;
  }
  return portFree;
}
/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 */
public static boolean available(int port) {
    if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}
    try {
        log.debug("{}: Checking if port open by trying to connect as a client", portNumber);
        Socket sock = new Socket("localhost", portNumber);          
        sock.close();
        log.debug("{}: Someone responding on port - seems not open", portNumber);
        return false;
    } catch (Exception e) {         
        if (e.getMessage().contains("refused")) {
            return true;
    }
        log.error("Troubles checking if port is open", e);
        throw new RuntimeException(e);              
    }
private static boolean available(int port) {
    System.out.println("--------------Testing port " + port);
    Socket s = null;
    try {
        s = new Socket("localhost", port);

        // If the code makes it this far without an exception it means
        // something is using the port and has responded.
        System.out.println("--------------Port " + port + " is not available");
        return false;
    } catch (IOException e) {
        System.out.println("--------------Port " + port + " is available");
        return true;
    } finally {
        if( s != null){
            try {
                s.close();
            } catch (IOException e) {
                throw new RuntimeException("You should handle this error." , e);
            }
        }
    }
}
private static boolean available(int port) {
    try (Socket ignored = new Socket("localhost", port)) {
        return false;
    } catch (IOException ignored) {
        return true;
    }
}
Sigar sigar = new Sigar();
int flags = NetFlags.CONN_TCP | NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT;             NetConnection[] netConnectionList = sigar.getNetConnectionList(flags);
for (NetConnection netConnection : netConnectionList) {
   if ( netConnection.getLocalPort() == port )
        return false;
}
return true;
            Socket Skt;
            String host = "localhost";
            int i = 8983; // port no.

                 try {
                    System.out.println("Looking for "+ i);
                    Skt = new Socket(host, i);
                    System.out.println("There is a Server on port "
                    + i + " of " + host);
                 }
                 catch (UnknownHostException e) {
                    System.out.println("Exception occured"+ e);

                 }
                 catch (IOException e) {
                     System.out.println("port is not used");

                 }
boolean isPortOccupied(int port) {
    DatagramSocket sock = null;
    try {
        sock = new DatagramSocket(port);
        sock.close();
        return false;
    } catch (BindException ignored) {
        return true;
    } catch (SocketException ex) {
        System.out.println(ex);
        return true;
    }
}
import java.net.DatagramSocket;
import java.net.BindException;
import java.net.SocketException;
public static boolean isTcpPortAvailable(int port) {
    try (ServerSocket serverSocket = new ServerSocket()) {
        // setReuseAddress(false) is required only on OSX, 
        // otherwise the code will not work correctly on that platform          
        serverSocket.setReuseAddress(false);
        serverSocket.bind(new InetSocketAddress(InetAddress.getByName("localhost"), port), 1);
        return true;
    } catch (Exception ex) {
        return false;
    }
}       
/**
 * Check to see if a port is available.
 *
 * @param port
 *            the port to check for availability.
 */
public static boolean isPortAvailable(int port) {
    try (var ss = new ServerSocket(port); var ds = new DatagramSocket(port)) {
        return true;
    } catch (IOException e) {
        return false;
    }
}