简体   繁体   中英

Java Networking Issue

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.

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
  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM