java 判断远程主机的端口是否开通
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Objects;
public class SocketJava {
public static void main(String[] args) {
if(Objects.isNull(args) || args.length != 2){
System.out.println("Usage: IP Port");
return;
}
String ip = args[0];
int port = Integer.valueOf(args[1]);
try(Socket socket = new Socket()){
socket.connect(new InetSocketAddress(ip,port));
System.out.println(ip + " " + port + " success");
}catch (IOException e){
System.out.println(e.getMessage());
System.out.println();
System.out.println(ip + " " + port + " failed");
}
}
}
- 程序员就不应该用Telnet,使用TCP
root@c8f13428e462:/
root@c8f13428e462:/
SocketJava.class SocketJava.java bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@c8f13428e462:/
Usage: IP Port
root@c8f13428e462:/
root@c8f13428e462:/
Usage: IP Port
root@c8f13428e462:/
Connection refused (Connection refused)
192.168.11.119 8080 failed
root@c8f13428e462:/
192.168.11.119 13306 success
root@c8f13428e462:/
root@c8f13428e462:/
Exception in thread "main" java.lang.IllegalArgumentException: port out of range:138083
at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:143)
at java.net.InetSocketAddress.<init>(InetSocketAddress.java:224)
at SocketJava.main(SocketJava.java:16)
root@c8f13428e462:/
Connection refused (Connection refused)
192.168.11.119 8083 failed
root@c8f13428e462:/
192.168.11.119 18083 success
- 清爽啊