Java 端口扫描器缺少打开的端口
Java Port Scanner is Missing Open Ports
作为一个更大项目的切线,我正在尝试实现一些代码来扫描开放端口,以便我最终可以 运行 进行数据包测试。目前我只是想连接到 google.com 并且我知道端口 80 接受连接,所以我已经格式化我的代码以扫描一系列端口,包括这个端口。然而,虽然我可以直接连接到端口 80,但扫描器代码似乎会绕过它。我想知道是否有人有任何想法?
public class PortScanner {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
String host = "www.google.com";
System.out.println("Attempting to connect to " + host + " at port 80...");
try {
socket.connect(new InetSocketAddress(host, 80), 5000);
System.out.println("Connection Successful.");
}
catch (Exception e) {
//Just bypass the exceptions for now
}
System.out.println("Scanning for open ports on " + host);
for (int i = 75; i < 85; i++) {
System.out.println("Attempting to connect to " + host + " on port " + i);
try {
socket.connect(new InetSocketAddress(host, i), 5000);
System.out.println("Open port found at " + i);
}
catch (ConnectException e) {
System.err.println("Connect Exception Thrown on Port " + i);
continue;
}
catch (SocketException e) {
System.err.println("Socket Exception Thrown on Port " + i);
continue;
}
catch (UnknownHostException e) {
System.err.println("Unknown host at " + host);
System.exit(1);
}
}
System.out.println("Closing connection to " + host);
socket.close();
System.out.println("Connection closed");
}
}
事实上,循环报告在端口 80 抛出 SocketException:
Attempting to connect to www.google.com at port 80...
Connection Successful.
Scanning for open ports on www.google.com
Socket Exception Thrown on Port 75
Socket Exception Thrown on Port 76
Socket Exception Thrown on Port 77
Socket Exception Thrown on Port 78
Socket Exception Thrown on Port 79
Socket Exception Thrown on Port 80
Socket Exception Thrown on Port 81
Socket Exception Thrown on Port 82
Socket Exception Thrown on Port 83
Attempting to connect to www.google.com on port 75
Socket Exception Thrown on Port 84
Attempting to connect to www.google.com on port 76
Attempting to connect to www.google.com on port 77
Attempting to connect to www.google.com on port 78
Attempting to connect to www.google.com on port 79
Attempting to connect to www.google.com on port 80
Attempting to connect to www.google.com on port 81
Attempting to connect to www.google.com on port 82
Attempting to connect to www.google.com on port 83
Attempting to connect to www.google.com on port 84
Closing connection to www.google.com
Connection closed
控制台日志按它们出现的顺序出现这一事实让我想知道连接尝试是否进入得太快,从而导致程序错过可用的端口,但我不太确定该怎么做。
任何想法表示赞赏!
一个Socket
最多只能用于1个活动连接!由于您在端口扫描开始时连接(并且只在结束时关闭它),任何进一步的连接尝试(通过调用 connect()
方法)将抛出 SocketException
.
打印您的 SocketException
的消息以验证这将是:"already connected"
.
建议:在每次尝试之前创建一个新的 Socket
。请注意,Socket
不能 重复使用 用于 multiple/new 连接。引用自 Socket.close()
的 Javadoc:
Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.
作为一个更大项目的切线,我正在尝试实现一些代码来扫描开放端口,以便我最终可以 运行 进行数据包测试。目前我只是想连接到 google.com 并且我知道端口 80 接受连接,所以我已经格式化我的代码以扫描一系列端口,包括这个端口。然而,虽然我可以直接连接到端口 80,但扫描器代码似乎会绕过它。我想知道是否有人有任何想法?
public class PortScanner {
public static void main(String[] args) throws IOException {
Socket socket = new Socket();
String host = "www.google.com";
System.out.println("Attempting to connect to " + host + " at port 80...");
try {
socket.connect(new InetSocketAddress(host, 80), 5000);
System.out.println("Connection Successful.");
}
catch (Exception e) {
//Just bypass the exceptions for now
}
System.out.println("Scanning for open ports on " + host);
for (int i = 75; i < 85; i++) {
System.out.println("Attempting to connect to " + host + " on port " + i);
try {
socket.connect(new InetSocketAddress(host, i), 5000);
System.out.println("Open port found at " + i);
}
catch (ConnectException e) {
System.err.println("Connect Exception Thrown on Port " + i);
continue;
}
catch (SocketException e) {
System.err.println("Socket Exception Thrown on Port " + i);
continue;
}
catch (UnknownHostException e) {
System.err.println("Unknown host at " + host);
System.exit(1);
}
}
System.out.println("Closing connection to " + host);
socket.close();
System.out.println("Connection closed");
}
}
事实上,循环报告在端口 80 抛出 SocketException:
Attempting to connect to www.google.com at port 80...
Connection Successful.
Scanning for open ports on www.google.com
Socket Exception Thrown on Port 75
Socket Exception Thrown on Port 76
Socket Exception Thrown on Port 77
Socket Exception Thrown on Port 78
Socket Exception Thrown on Port 79
Socket Exception Thrown on Port 80
Socket Exception Thrown on Port 81
Socket Exception Thrown on Port 82
Socket Exception Thrown on Port 83
Attempting to connect to www.google.com on port 75
Socket Exception Thrown on Port 84
Attempting to connect to www.google.com on port 76
Attempting to connect to www.google.com on port 77
Attempting to connect to www.google.com on port 78
Attempting to connect to www.google.com on port 79
Attempting to connect to www.google.com on port 80
Attempting to connect to www.google.com on port 81
Attempting to connect to www.google.com on port 82
Attempting to connect to www.google.com on port 83
Attempting to connect to www.google.com on port 84
Closing connection to www.google.com
Connection closed
控制台日志按它们出现的顺序出现这一事实让我想知道连接尝试是否进入得太快,从而导致程序错过可用的端口,但我不太确定该怎么做。
任何想法表示赞赏!
一个Socket
最多只能用于1个活动连接!由于您在端口扫描开始时连接(并且只在结束时关闭它),任何进一步的连接尝试(通过调用 connect()
方法)将抛出 SocketException
.
打印您的 SocketException
的消息以验证这将是:"already connected"
.
建议:在每次尝试之前创建一个新的 Socket
。请注意,Socket
不能 重复使用 用于 multiple/new 连接。引用自 Socket.close()
的 Javadoc:
Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.