使用 Java 检查开放端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4883771/
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
Check for open ports with Java
提问by GPX
Using Java, how do I check if a specific TCP/IP port is open and not blocked by a firewall?
使用 Java,如何检查特定 TCP/IP 端口是否打开且未被防火墙阻止?
回答by Kel
If by "port is open" you mean, that this port can be used by your server application, then you can just do:
如果“端口已打开”是指您的服务器应用程序可以使用该端口,那么您可以执行以下操作:
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.out.println("Could not listen on port: " + port);
// ...
}
IOException will be thrown, if you cannot open server socket on this port.
如果您无法在此端口上打开服务器套接字,则会抛出 IOException。
If by "not blocked by a firewall" you mean, that this port can be accessed from hosts outside your network, then there's no straight way to check this without trying to open connection to your host:port from outside network. There may be other firewalls / NATs between host where your service is started, and host, which may try to connect to you service.
如果“未被防火墙阻止”是指可以从网络外部的主机访问该端口,那么如果不尝试从外部网络打开到您的主机的连接,就没有直接的方法来检查这一点。启动您的服务的主机和可能尝试连接到您的服务的主机之间可能存在其他防火墙/NAT。
There are some common techniques which allow to check service accessibility from outside network, see, for example, NAT traversal.
有一些常用技术可以检查来自外部网络的服务可访问性,例如,参见NAT 遍历。
回答by Andreas Dolk
Check if a port is open for inboundconnections? You'll have to ask the firewall. Suppose, you listen to a port but this port is blocked by the firewall, then you'll wait "forever".
检查端口是否为入站连接打开?你得问防火墙。假设,您侦听一个端口,但该端口被防火墙阻止,那么您将“永远”等待。
The only way out - run a small server outside your network/your machine and ask it remotly to establish a connection to a given port on your machine. The remote server can reply (on a different channel) if it was able to connect or not.
唯一的出路 - 在您的网络/您的机器之外运行一个小型服务器,并远程要求它与您机器上的给定端口建立连接。远程服务器可以回复(在不同的频道上)是否能够连接。
Another idea: use one of the many port test services on the web. googling "port test online" gives some results.
另一个想法:使用网络上的众多端口测试服务之一。谷歌搜索“在线端口测试”给出了一些结果。
回答by AlexR
Socket allows connection between 2 machines in the network. There may be several filrewalls on this way:
套接字允许网络中两台机器之间的连接。这种方式可能有几个文件墙:
- personal firewall on both sides
- firewalls of the companies on both sides.
- firewalls of the ISPs on both sides.
- 双方个人防火墙
- 双方公司的防火墙。
- 双方 ISP 的防火墙。
Firewalls may be configured to block
防火墙可以配置为阻止
- certain IPs
- certain ports
- certain protocols
- traffic direction (in/out bound)
- 某些 IP
- 某些端口
- 某些协议
- 交通方向(进/出)
Moreover 2 different situations look the same from TCP point of view:
此外,从 TCP 的角度来看,两种不同的情况看起来是一样的:
- server is not listening to the port
- firewall blocks the connection (see above)
- 服务器没有监听端口
- 防火墙阻止连接(见上文)
Shortly first you have to decide what do you want to test. If for example you just want to know that you can connect to specific port on specific machine call new Socket(host, port)
and catch exception. If you want to distinguish between situation that firewall bothers you or the remote has does not respond it is not enough.
很快,您必须决定要测试什么。例如,如果您只想知道您可以连接到特定机器调用上的特定端口new Socket(host, port)
并捕获异常。如果你想区分防火墙打扰你或远程没有响应的情况是不够的。
In this case you need some other reference. For example you know that remote host has HTTP server on it and some other proprietary server that can be blocked by firewall you can first establish HTTP connection (to check that host is alive) and then try to connect to the socket. If HTTP works and socket does not probably firewall is blocking it.
在这种情况下,您需要一些其他参考。例如,您知道远程主机上有 HTTP 服务器和其他一些可以被防火墙阻止的专有服务器,您可以首先建立 HTTP 连接(以检查该主机是否活动),然后尝试连接到套接字。如果 HTTP 工作并且套接字没有可能防火墙阻止它。