import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Main {
public static void main(String args[]){
System.out.println("端口 "+isPortUsing("127.0.0.1",8888));
}
/***
* 测试主机Host的port端口是否被使用
* @param host
* @param port
* @throws UnknownHostException
*/
public static boolean isPortUsing(String host,int port) throws UnknownHostException{
boolean flag = false;
InetAddress Address = InetAddress.getByName(host);
try {
Socket socket = new Socket(Address,port); //建立一个Socket连接
flag = true;
} catch (IOException e) {
}
return flag;
}
}
有些window Telnet不能正常使用,在这个情况下,可以使用Java测试端口是否可以使用