Java 网络问题

发布于 2024-10-19 11:33:36 字数 1179 浏览 1 评论 0原文

我们连接了一个路由器和 3 台电脑。

  1. PC1:192.168.1.2(无线)
  2. PC2:192.168.1.3(无线)
  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 成功。

  1. 我的设置有什么问题。
  2. 我该如何解决这个问题。

We have a router and 3 PCs connected.

  1. PC1: 192.168.1.2 (wireless)
  2. PC2: 192.168.1.3 (wireless)
  3. 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.

  1. What is the problem with my setup.
  2. How can I resolve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-10-26 11:33:36

我相信吉多在评论中的理论是正确的。请参阅此问题。我意识到链接的答案适用于 Linux/UNIX 主机,但我刚刚在 Windows 7 计算机上测试了您的代码,并验证它也将 TCP 数据包发送到 echo 端口 (7),而不是ICMP 回显请求。 (我在 Windows 7 机器上的 Java 版本 1.6.0_21 下进行了测试,其中 ver 报告了 Microsoft Windows [版本 6.1.7600]

) Windows 计算机可能会启用 Windows 防火墙。因此,可能会发生以下事件序列:

  1. 您的 Java 程序(无法发送和接收 ICMP 数据包来测试远程 Windows 主机的可达性)将尝试通过发送SYN(同步)数据包发送到端口 7
  2. 远程 Windows 主机将丢弃该数据包,而不是按照 RFC 793 如果没有服务正在侦听该端口。也就是说,根据 RFC,远程 Windows 主机应该使用 TCP 进行响应:
  • RST:如果端口未侦听连接
  • SYN+ACK:如果端口已打开并正在侦听连接
  1. 由于您的 Java 程序期望在步骤 (2) 中发生两件事之一,但实际上根本没有得到任何响应(因为 Windows 在到达 TCP 堆栈之前丢弃了 SYN 数据包),因此它错误地假设远程 Windows 主机是向下。

很难说“你的设置有问题”是什么。也许什么也没有。这取决于您尝试使用此功能的用途。你可能会以错误的方式处理这件事。您应该意识到 ping 并不总是 100% 准确的测试主机是否在线的方法。在您的情况下,如果您只对本地网络上的主机感兴趣,尝试 ping 主机后检查 ARP 表可能会更准确。

可能的解决方法:

  • 尝试暂时关闭其中一台其他计算机上的 Windows 防火墙,然后查看您的代码是否有效。
  • 编写一些执行单独进程的代码,例如运行 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 version 1.6.0_21 on a Windows 7 box where ver reported Microsoft Windows [Version 6.1.7600])

A typical Windows machine will likely have Windodws Firewall enabled. Therefore, the following sequence of events is likely:

  1. Your Java program (not being able to send and receive ICMP packets to test for the reachability of the remote Windows host) will attempt to initiate a TCP connection to the remote Windows host, by sending a SYN (synchronize) packet to port 7
  2. The remote Windows host will drop the packet, rather than responding with a RST packet as required by RFC 793 if no service was listening on that port. That is, the remote Windows host should have, per the RFC, have either responded with a TCP:
  • RST: if the port was not listening for connections
  • SYN+ACK: if the port was open and listening for connections
  1. Since your Java program was expecting one of the two things to happen in step (2) but instead got no response at all (since Windows dropped the SYN packet before it ever got to the TCP stack) it falsely assumes the remote Windows host is down.

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:

  • Try turning off the Windows firewall temporarily on one of the other machines and see if your code works.
  • Write some code that executes a separate process, for example runs the ping command line utility to test reachability.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文