Java 网络问题
我们连接了一个路由器和 3 台电脑。
- PC1:192.168.1.2(无线)
- PC2:192.168.1.3(无线)
- PC3:192.168.1.6
默认网关:192.168.1.1
当 PC3 尝试使用 此论坛帖子,它只返回IP默认网关的地址(该地址是唯一可到达的地址)。
我尝试增加 isReachable() 方法的超时。但它仍然只返回默认网关地址。
我尝试对各个 IP 地址执行此操作。
try {
InetAddress temp2 = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 1, (byte) 2});
if (temp2.isReachable(1100)) {
java.lang.System.out.println("IP Address: " + temp2.getHostAddress() + " has connection.");
}else{
java.lang.System.out.println("IP Address: " + temp2.getHostAddress() + " has no connection.");
}
} catch (Exception ex) {
java.lang.System.out.println("Error: " + ex.getMessage());
}
然而,在 PC1 和 PC2 IP 地址上执行操作时,我只得到了无连接状态。 (这意味着这些 IP 无法访问。)
但是当我在 Windows 控制台上 ping 它们时,这些 IP 已连接并且 ping 成功。
- 我的设置有什么问题。
- 我该如何解决这个问题。
We have a router and 3 PCs connected.
- PC1: 192.168.1.2 (wireless)
- PC2: 192.168.1.3 (wireless)
- PC3: 192.168.1.6
Default gateway: 192.168.1.1
When PC3 tried to find connected PCs using the code from this forum post, it only returns the IP address of the default gateway (That address is the only reachable address).
I tried increasing the timeout for the isReachable()
method. But still it returns only the default gateway address.
I tried doing this to the individual IP addresses.
try {
InetAddress temp2 = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 1, (byte) 2});
if (temp2.isReachable(1100)) {
java.lang.System.out.println("IP Address: " + temp2.getHostAddress() + " has connection.");
}else{
java.lang.System.out.println("IP Address: " + temp2.getHostAddress() + " has no connection.");
}
} catch (Exception ex) {
java.lang.System.out.println("Error: " + ex.getMessage());
}
Yet doing on those PC1 and PC2 IP addresses, I only got a no connection status. (Which means those IPs are not reachable.)
But when I ping them on my windows console those IPs are connected and pinging is successful.
- What is the problem with my setup.
- How can I resolve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信吉多在评论中的理论是正确的。请参阅此问题。我意识到链接的答案适用于 Linux/UNIX 主机,但我刚刚在 Windows 7 计算机上测试了您的代码,并验证它也将 TCP 数据包发送到
echo
端口 (7),而不是ICMP 回显请求。 (我在 Windows 7 机器上的 Java 版本1.6.0_21
下进行了测试,其中ver
报告了Microsoft Windows [版本 6.1.7600]
) Windows 计算机可能会启用 Windows 防火墙。因此,可能会发生以下事件序列:
很难说“你的设置有问题”是什么。也许什么也没有。这取决于您尝试使用此功能的用途。你可能会以错误的方式处理这件事。您应该意识到
ping
并不总是 100% 准确的测试主机是否在线的方法。在您的情况下,如果您只对本地网络上的主机感兴趣,尝试 ping 主机后检查 ARP 表可能会更准确。可能的解决方法:
ping
命令行实用程序来测试可达性。I believe guido is correct with his theory in the comments. See this question. I realize that the linked answer applies to Linux/UNIX hosts, but I just tested your code on a Windows 7 machine and verified that it, also, sends TCP packets to the
echo
port (7) rather than an ICMP echo request. (I tested this under Java version1.6.0_21
on a Windows 7 box wherever
reportedMicrosoft Windows [Version 6.1.7600]
)A typical Windows machine will likely have Windodws Firewall enabled. Therefore, the following sequence of events is likely:
It's hard to say what the "problem with your setup" is. Maybe nothing. It depends on what you're trying to use this functionality for. You might be going about it in the wrong way. You should realize that
ping
is not always a 100% accurate way to test if a host is online. In your case it might be more accurate to check the ARP table after trying to ping the host, if you're only interested in hosts on your local network.Possible workarounds:
ping
command line utility to test reachability.