FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


java question





Marcuzzo
Hi guys,
Is there a way to check if a remote machine is available.
I've tried the isReachable method but that one will always give me "false" as output
the code I'm using is:
Code:
InetAddress address = InetAddress.getByName(remotehost);
       System.out.println("Name: " + address.getHostName());
       System.out.println("Addr: " + address.getHostAddress());
       System.out.println("Reach: " + address.isReachable(5000));

remotehost is parsed from a supplied argument.
is there any other way, because I think that the echo port (7) is beeing blocked here on our lan and I need this because I work on 4 different machines and I need to check if the machine is online before I attempt to copy files from it.
thanks in advance
Peterssidan
What happen if you increase the timeout value you pass to isReachable to something bigger?
Marcuzzo
Peterssidan wrote:
What happen if you increase the timeout value you pass to isReachable to something bigger?

Hi Peterssidan,
I knew I could count on you.

I changed the code to supply the timeout in the second arg, like so:
Code:
import java.io.*;
import java.net.*;

public class ReachableTest {
 public static void main(String [] args)
{
    String remotehost = "";
    int timeout = 5000;
   
    Console console = System.console();
       
    if (console == null)
    {
        System.err.println("No console.");
        System.exit(1);
    }
       
    if ( args.length == 2 )
    {
        System.out.println ("Setting remotehost to: " + args[0] );
        remotehost = "" + args[0];
        timeout =  Integer.parseInt( args[1] );
    }
    else
    {
        remotehost = console.readLine("Please enter the name of the host: ");
        System.out.println("Defaulting timeout to " + timeout );
    }
   
     try {
         
       InetAddress address = InetAddress.getByName(remotehost);
       //~ System.out.println("Name: " + address.getHostName());
       //~ System.out.println("Addr: " + address.getHostAddress());

        System.out.print("Check Addr: " + address.getHostAddress());
        System.out.print(", Reachable: " + address.isReachable(timeout) + " timeout: " + timeout);
     }
     catch (UnknownHostException e) {
       System.err.println("Unable to lookup " + remotehost);
     }
     catch (IOException e) {
       System.err.println("Unable to reach " + remotehost);
     }
   }
}


this is the output
Code:
H:\RomProfile>java -cp . ReachableTest A05904 25000
Setting remotehost to: A05904
Check Addr: 10.31.67.6, Reachable: false timeout: 25000
H:\RomProfile>java -cp . ReachableTest google.com 25000
Setting remotehost to: google.com
Check Addr: 74.125.79.104, Reachable: false timeout: 25000
H:\RomProfile>java -cp . ReachableTest google.com 50000
Setting remotehost to: google.com
Check Addr: 74.125.79.104, Reachable: false timeout: 50000
H:\RomProfile>java -cp . ReachableTest A05904 50000
Setting remotehost to: A05904
Check Addr: 10.31.67.6, Reachable: false timeout: 50000
H:\RomProfile>


I think this is filtered out in our network here.

If there is no other way I could try something like this:
Code:
String pingCmd = "ping " + ip;
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);

and then regex the result out , but I would prefer another way if it is possible in java

thanks in advance
Peterssidan
I think the best way is to just connect the way you intend to be using it later. So if you want to use TCP connection, try to set up a TCP channel. If it fails the computer is not reachable. I think this is the most reliable way because you never know what ports are being blocked. Even if isReachable returns true you can not be sure it will accept other kinds of connections.
Marcuzzo
okido, thanks Perterssidan, I'll try it that way.
AftershockVibe
This is out of my area of expertise here (I have not used these methods in Java), but thought it may be worth noting something I found when doing similar things in C++ (using Qt).

They also had some sort of isReachable() function, this took a single parameter which was the machines "address" (with custom datatype). Now, address is actually very non-specific. Turns out that if you specified a DNS address (i.e. www.example.com) then the function would always return false because resolving DNS wasn't supported.

This wasn't apparent when because this was perfectly fine elsewhere when using the same Address datatype.

Basically what I'm saying is check what form your "remoteHost" variable takes (DNS? Network host name? NetBIOS??!) and then ensure that all of InetAddress's methods are ok with it.
Related topics
Java download question
Where to find Java Scripts
Java Has Failed!
HTML/Java Question
Whos is the FUTURE.... JAVA or .NET
N00b Java Question
Java Support
java help
Java Servlet IDE and development Environmnet
ASP session.timeout question
a little question
Online test
I hate Java but I have a question
Custom Operating System in C#, C++, or Java
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.