如何扫描远程主机使用的端口?
这是我到目前为止得到的代码...
import java.net.*;
public class PortScanner {
public static void main(String args[]) {
int startPortRange = 0;
int stopPortRange = 0;
startPortRange = Integer.parseInt(args[0]);
stopPortRange = Integer.parseInt(args[1]);
for (int i = startPortRange; i <= stopPortRange; i++) {
try {
Socket ServerSok = new Socket("127.0.0.1", i);
System.out.println("Port in use: " + i);
ServerSok.close();
} catch (Exception e) {
System.out.println("Port not in use: " + i);
}
}
}
} 例如,在 smtp 服务器上,系统使用端口 25,我需要扫描连接到 smtp 服务器的远程客户端上的端口。我怎样才能做到这一点?
请帮我。
this is the code that i get so far...
import java.net.*;
public class PortScanner {
public static void main(String args[]) {
int startPortRange = 0;
int stopPortRange = 0;
startPortRange = Integer.parseInt(args[0]);
stopPortRange = Integer.parseInt(args[1]);
for (int i = startPortRange; i <= stopPortRange; i++) {
try {
Socket ServerSok = new Socket("127.0.0.1", i);
System.out.println("Port in use: " + i);
ServerSok.close();
} catch (Exception e) {
System.out.println("Port not in use: " + i);
}
}
}
}
for example on smtp server the system use port 25, and i need to scan port on remote client that connected to the smtp server. how can i do that?
please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码应如下所示:
Your code should look like this:
如果我理解你的问题:
你吗想用Java发现F中的T吗?你不能!
您必须阅读打开套接字的操作系统表。这是一个依赖于操作系统的操作,java 不支持它。
否则,如果F是S,则可以更改S的代码来发现T。如果S使用java.net.ServerSocket类,则可以在返回的Socket实例上使用getPort()搜索ServerSocket.accept()并发现T。
安德里亚律师事务所
If I understood your problem:
Do you want to discover T in F using Java? You cannot!
You have to read the operative system table of open sockets. This is an operative system dependent operation and it is not supported by java.
Otherwise if F is S, you can change the code of S to discover T. If S uses java.net.ServerSocket class, you can search ServerSocket.accept() and discover T using getPort() on the returned Socket instance.
LLP, Andrea
首先,这种方法存在一些方法上的问题,因为端口可以被使用,但不能监听。
如果您实际上只是在寻找侦听端口,并且想要使用上面的方法,那么至少生成一些线程,这样您就不必等待每个端口依次超时。
最好的方法是模仿 syn 扫描之类的东西,而不是进行完整的 TCP 握手,但我不确定您是否可以在 Java 中达到如此接近的效果。
不过,这是一个很好的问题。
Firstly there are some methodological problems with this way of doing it since a port can be in use, but not listening.
If you're actually just looking for listening ports, and you want to use the approach above, at least spawn off some threads so that you don't have to wait for each port to time out sequentially.
The best way would be to mimic something like a syn scan rather than doing a full TCP handshake, but I'm not sure you can get that close to the metal in Java.
Neat question though.