import java.net.*;
import java.io.IOException;
public class ScannerPort //extends Thread {
{
static String ip = "xxx";
public static void main(String[] args) {
for (int i = 10000; i < 12000; i++) {
test(i);
}
}
public static void test(int port) {
SocketAddress socketAddress = new InetSocketAddress(ip, port);
try {
Socket client = new Socket(); //new Socket(ip, port);
client.connect(socketAddress, 100);//连不上的0.1秒断掉连接
//client.setSoTimeout(100); //其实这个是响应阻塞超时,是client和服务端建立连接后,等待接收数据的超时时间,真正的连接超时不是在这里设置
//如果client不为空,说明该端口对外开放, 否则该端口没有在监听的服务,或者是该端口对外屏蔽。
if (client != null) {
System.out.println(" ------------- Port :" + port + " is OK!!!");
} else {
System.out.print(port + ",");
}
} catch (Exception e2) {
System.out.print(port + ",");
}
}
}
socket 连接超时处理 java_qq_4219790085的博客-CSDN博客_socket连接超时怎么解决啊
|