• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2349
  • Last Modified:

isReachable and IP Address

Can someone provide an example on how to use isReachable() in InetAddress that doesn't use getByName?  Not all of my devices use hostnames/DNS and I would like to test just on the ip address.  Here's how I'm currently using it.

InetAddress.getByName("192.168.1.1").isReachable(5000)

Thanks
0
jhughes4
Asked:
jhughes4
  • 12
  • 11
  • 2
  • +3
1 Solution
 
aozarovCommented:
That should still work.
0
 
aozarovCommented:
From the javadoc for getByName:
The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

0
 
sudhakar_koundinyaCommented:
The current procedure  what you are doing is right

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sudhakar_koundinyaCommented:
InetAddress.getByName("192.168.1.1").isReachable(5000)

or

InetAddress.getByName("dev.comp1").isReachable(5000)

both are valid
0
 
jhughes4Author Commented:
Thanks for the quick responses.  The reason I thought the problem was with the hostname was that I'm able to ping this router which is on the same network as other routers, but isReachable returns false.  The other two routers as well as the other PCs return true, the only difference I could find was that this router that returns false doesn't have a hostname.  Any ideas as to what else could be wrong?
0
 
petiexCommented:
If the code you posted does not work, it might be the version of java you are running. The isReachable() method is new in java 1.5, so if you have java 1.4.x installed, the method won't exist and you'll get a compile error.
0
 
jhughes4Author Commented:
I have 1.5.0_02 & it works with the other routers and PCs.
0
 
aozarovCommented:
hostname vs ip should not be the problem as you can verify that by calling
InetAddress.getByAddress(new byte[]{(byte)192, (byte)168, (byte)1, (byte)1}).isReachable(5000);
So you say you can ping 192.168.1.1 sucessfuly from the same machine you apply the isReachable?
 
0
 
cjardCommented:
ping the router from the machine you are having problems with (in java).

if the ping returns "destination unreachable", "no route to host" or takes longer than 5 seconds, then it will fail for java.

if ping works, then it is probably a security issue with java; isReachable issues icmp echo requests, failing that it tries a tcp connection to the echo (ping) port.. port 7.. not all machines that respond to pings actually open port 7 for tcp/ip.. usually it's just icmp/ip

the machine youre running this on; does it have a firewall? does java have any security policies in place? it is not enough to assume that because a machine has internet connectivity through a particular router, that neither the machine and the router are firewalling pings. however, using the ping command from a dos prompt would fail if a firewall or security restriction were active

0
 
jhughes4Author Commented:
Sorry for the late response.  
aozarov,
   InetAddress.getByAddress(new byte[]{(byte)192, (byte)168, (byte)1, (byte)1}).isReachable(5000); fails as well.  Yes I'm able to ping the router from the same machine that's using isReachable.

cjard,
  When you say ping in Java, I'm assuming you mean to use Runtime, which I did and was able to ping ok.  The machine I'm pinging is a router.  I'm able to ping another router on the same network that's the same model as this router.  I've even tried to reset the router to the default values, but isReachable still doesn't work.  Would the security policies apply in this case?

Thanks again.
0
 
aozarovCommented:
>> InetAddress.getByAddress(new byte[]{(byte)192, (byte)168, (byte)1, (byte)1}).isReachable(5000); fails as well.
I know, I was just trying to prove the point that hostname translation is not the problem here.

Try to run your java program with full permissions (like root), though it is surprising that you managed to use the same java application to check isReachable to other routers sucessfuly....
0
 
jhughes4Author Commented:
I'm running on W2K, and I'm logged in as Administrator.
0
 
jhughes4Author Commented:
isReachable works on PCs that are connected to/behind this router, so that doesn't mean it's a firewall issue does it???
0
 
aozarovCommented:
>> so that doesn't mean it's a firewall issue does it???
It sounds so, but then you should not be able to ping sucessfully from your W2K machine to the same router that your application fails.
0
 
jhughes4Author Commented:
Yep, I'm able to ping the router and the other PCs behind that router and even use isReachable for those PCs, but not just the router.  I thought another problem could be that the router was plugged into a hub, but when I plugged the router directly into my main router I was still unable to see it with isReachable.  I know that shouldn't matter, but didn't want to leave any stone unturned.  I also checked the router with tracert and was able to see it.
0
 
jhughes4Author Commented:
Do you have any examples on how to use isReachable(NetworkInterface interface, int ttl, int timeout)?  Thanks again
0
 
aozarovCommented:
That should be similar to the above.
NetworkInterface is relevant if you have multiple network cards on your machine.
In that case you can pick one by calling NetworkInterface.getByInetAddress or getByName (the address used by that specific NIC on your machine).
0
 
jhughes4Author Commented:
so
  isReachable(NetworkInterface.getByInetAddress("ipaddress of machine running java"), 50, 50)?

Sorry for the beginner question.
0
 
aozarovCommented:
almost:  isReachable(NetworkInterface.getByInetAddress(InetAddress.getByName("ipaddress of machine running java")), 50, 50) // assuming you want only 50 milliseconds timeout
0
 
aozarovCommented:
actually you can also do:
isReachable(NetworkInterface.getByName("ipaddress of machine running java"), 50, 50)
0
 
jhughes4Author Commented:
I guess if there are no other suggestions I'm going to have to do a ping if the isReachable returns failure.
0
 
aozarovCommented:
Before invoking Runtime.getRuntime().exec("ping.... as a fallback
you can also try to using:
http://java.sun.com/j2se/1.4.2/docs/api/java/net/DatagramSocket.html#connect(java.net.InetAddress,%20int)
If you know a standard UDP port that is going to be available. (e.g. 7/udp    Echo,  13/udp    Daytime (RFC 867)
, 37/udp    Time, ...)
0
 
jhughes4Author Commented:
Thanks.  Can you provide a snippet on how best to implement that connect without sending any data?


thanks again
0
 
aozarovCommented:
Exception (if not reachable) should be thrown for both read and write operations.
so you can call setSoTimeout( -> to very small value so you will bail out imediatly (this might also trigger the exception, but not sure about it)
and then call to read.
0
 
jhughes4Author Commented:
Can you tell me what I'm doing wrong?  Even if I pass an invalid IP address, isBound and isConnected return true.  Thanks

DatagramSocket aSocket = null;

        try
            {
            aSocket = new DatagramSocket();
            byte [] m = args[0].getBytes();

            InetAddress aHost = InetAddress.getByName(args[1]);
            

      DatagramPacket request = new DatagramPacket(m,  args[0].length(), aHost, serverPort);
            aSocket.send(request);
            //aSocket.connect(aHost, 7);

            System.out.println("State:" + aSocket.isBound());
0
 
RomanRegaCommented:
Datagram packets are UDP.
There's no connection involved so. It's not TCP.

I think that if you do aSocket.isConnect() what you get is just information about if you already called aSocket.connect() or not.

In the other hand isBound() means that aSocket has already got a reseverved port to work (on your local machine). I you create the aSocket like this
aSocket = new DatagramSocket();
the socket will be bound to any free avaible port on your machine.
So it is normal that you get isBound() == true.


0
 
jhughes4Author Commented:
So DatagramSocket isn't going to tell me if an IP address is reachable or exists?  Are there any other options I could look at before going to ping?

thanks
0
 
aozarovCommented:
>> There's no connection involved so. It's not TCP.
RomanRega, actually in 1.4 an ICMP request is performed to check reachability if you use this method:
http://java.sun.com/j2se/1.4.2/docs/api/java/net/DatagramSocket.html#connect(java.net.InetAddress,%20int)

jhughes4, you should do something like that:

DatagramSocket aSocket = null;

       try
          {
          aSocket = new DatagramSocket();
          byte [] m = args[0].getBytes();

          InetAddress aHost = InetAddress.getByName(args[1]);
         

          DatagramPacket request = new DatagramPacket(m,  args[0].length());
          aSocket.connect(aHost, 7);
           // now call either send or receive to see if connected
          aSocket.send(request);
          // if you got here then probably the other side is alive
}
catch (PortUnreachableException ex)
{
          // if you got here then it the other side is not alive
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 12
  • 11
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now