java 测试远程端口是否在使用中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5226905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test if remote port is in use
提问by Jeffrey Blattman
I have the need to discover open ports on a remote server. I'm wondering if this is possible. I was thinking I'd open a socket, and if this succeeds, it means that it's used ... otherwise, if I get an exception, then it is not used.
我需要发现远程服务器上的开放端口。我想知道这是否可能。我在想我会打开一个套接字,如果成功,就意味着它被使用了……否则,如果我得到一个异常,那么它就不会被使用。
For example,
例如,
public boolean isActive() {
Socket s = null;
try {
s = new Socket();
s.setReuseAddress(true);
SocketAddress sa = new InetSocketAddress(this.host, this.port);
s.connect(sa, 3000);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
}
}
}
return false;
}
is this a viable approach?
这是一种可行的方法吗?
采纳答案by Andrew White
Does this have to be done in Java? There are tools for that (Nmap). Else, your method will "work" but I'm not sure how useful this will be.
这是否必须在Java中完成?有工具可以做到这一点(Nmap)。否则,您的方法将“有效”,但我不确定这会有多大用处。
Be warned, firewalls can do some tricky things. Such as allow the connections to establish but do nothing with it so it appears as if the port is open but the firewall is actually blocking all traffic. Or, some ports will only be open to requests from a certain IP range, subnet, or physical device.
请注意,防火墙可以做一些棘手的事情。例如允许建立连接但不对其进行任何操作,因此看起来好像端口是打开的,但防火墙实际上阻止了所有流量。或者,某些端口将仅对来自特定 IP 范围、子网或物理设备的请求开放。
回答by Alain Pannetier
FWIW, a Java solution I use from times to times (better than telnet: supports timeout).
FWIW,我不时使用的 Java 解决方案(比 telnet 更好:支持超时)。
package com.acme.util;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
public class CheckSocket {
public static void main(String[] args) {
int exitStatus = 1 ;
if (args.length != 3) {
System.out.println("Usage: CheckSocket node port timeout");
} else {
String node = args[0];
int port = Integer.parseInt(args[1]);
int timeout = Integer.parseInt(args[2]);
Socket s = null;
String reason = null ;
try {
s = new Socket();
s.setReuseAddress(true);
SocketAddress sa = new InetSocketAddress(node, port);
s.connect(sa, timeout * 1000);
} catch (IOException e) {
if ( e.getMessage().equals("Connection refused")) {
reason = "port " + port + " on " + node + " is closed.";
};
if ( e instanceof UnknownHostException ) {
reason = "node " + node + " is unresolved.";
}
if ( e instanceof SocketTimeoutException ) {
reason = "timeout while attempting to reach node " + node + " on port " + port;
}
} finally {
if (s != null) {
if ( s.isConnected()) {
System.out.println("Port " + port + " on " + node + " is reachable!");
exitStatus = 0;
} else {
System.out.println("Port " + port + " on " + node + " is not reachable; reason: " + reason );
}
try {
s.close();
} catch (IOException e) {
}
}
}
}
System.exit(exitStatus);
}
}