Trying to understand java.net.InetAddress.isReachable() method
Hey guys,
I'm just testing some of the Java 6 API classes. So I'm playing with the isReachable() method.
I keep getting a syntax error when I try to use the method.
Let me show you what I'm trying to do:
import java.net.*;
import java.util.Scanner;
class NetTest {
public static void main(String[] args) {
Scanner netScan = new Scanner(System.in);
String myNet = netScan.next();
try {
InetAddress myAddr = InetAddress.getByName(myNet).isReachable(null, 0, 100);
}
catch (java.net.UnknownHostException exc) {
System.out.println("Can't find the host");
}
}
-
I get the syntax error for the line with .isReachable saying:
"Type mismatch: cannot convert from boolean to InetAddress"
The rest works, just not the .isReachable. I've looked at the usage of the method in the API, and I guess I'm Just not getting it or something. Can somebody help? Much appreciated.
[994 byte] By [
command0a] at [2007-11-26 17:13:18]

# 1
isReachable() returns a boolean. You're trying to assign the result of the method call to a variable of type InetAddress. Exactly like the compiler says, really.
# 2
InetAddress.getByName(myNet).isReachable(null, 0, 100);
Maybe I can describe as :
You want to "ping" a machine, that has an InetAddress obtained from hostname (myNet).
meanwhile (null, 0, 100) :
null --> through all network interface
0 --> only 1 time trying
100 --> 100ms for timeout, for each trial
This method, will return a boolean value, which true if can ping, false if already reach timeout and the trying time.
Maybe my explanation is not that clear, but hopefully can make u understand it better.
# 3
> null --> through all network interface
Correct
> 0 --> only 1 time trying
Incorrect. 0 -> default TTL (number of hops) (default is unspecified)
> 100 --> 100ms for timeout, for each trial
Incorrect. 100 -> 100ms for the total timeout. There is only one trial.
All this is as per the Javadoc. You should both read it some time ;-)
ejpa at 2007-7-8 23:41:14 >

# 4
> InetAddress.getByName(myNet).isReachable(null, 0,
> 100);
>
> Maybe I can describe as :
> You want to "ping" a machine, that has an InetAddress
> obtained from hostname (myNet).
>
> meanwhile (null, 0, 100) :
> null --> through all network interface
> 0 --> only 1 time trying
> 100 --> 100ms for timeout, for each trial
>
> This method, will return a boolean value, which true
> if can ping, false if already reach timeout and the
> trying time.
>
> Maybe my explanation is not that clear, but hopefully
> can make u understand it better.
As well as being mostly garbage, this all has nothing to do with the OPs question regarding his compiler error.
# 5
Hey ejp,
Good to hear from you again.
Ok, so I am looking at the API, and trying to understand how it explains the .isReachable explain it "RETURNS" a boolean. So with that logic in mind the arguments for the method are not necessarily boolean's then right?
So the line:
InetAddress myAddr = InetAddress.getHostByName("yahoo.com").isReachable(null);
return true;
Is what would logically make sense, but I still continue to get compiler errors.
Any thoughts?
Can you maybe give me an example of how it should logically work when using the .isReachable method? It would definitely help.
# 6
> Is what would logically make senseNone of this is logical or makes even the slightest sense.See reply #1. The isReachable() method returns a boolean, we all agree on that, so why would you trying to store the result in an InetAddress()? Store it in a boolean.
ejpa at 2007-7-8 23:41:14 >

# 7
Ok, so I understand it returns a boolean. That part is as clear as day.
But the part that is confusing me, is the arguments for the .isReachable() method. So you say, null is correct as it states in the API.
However, how it handles the arguments to return a boolean is where I'm being thrown off.
So instead of telling me that's it's a boolean which is obvious, could you help me, by giving me an example of the .isReachable method in action?
# 8
> So you say, null is correct as it states in the API.
I have said no such thing regarding your code, which certainly doesn't correspond to what it states in the API at all. I was answering another poster regarding the interpretation of a null NetworkInterface parameter. (In fact it was the only thing he got right.)
> However, how it handles the arguments to return a
> boolean is where I'm being thrown off.
I don't know what this even means.
Your problems are (a) you're not providing the correct argument(s), and (b) you're not assigning the result to the correct type.
This is elementary Java programming, and nothing to do with networking at all really.
A more useful question is, why do you think you need to use this method at all? Just trying to connect to whatever you're trying to connect to is infintely simpler and more reliable. You have to handle failures there anyway so isReachable() rarely adds any value.
ejpa at 2007-7-8 23:41:14 >

# 9
Ok, well, the use of this method based off the last thread we last spoke does not actually stand in for the "true" ICMP ping echo, but I'm trying to get down a few packages in the API down packed, because the nature of my business involves some sort of networking to some extent.
I am preparing to fill in as a role of a QA analyst, checking for inconsistancies in Java Servlets (JSP). The servlets act as a complete site, allowing connectivity to SQL servers, and data feeds from various vendors.
But specifically, I'm trying to understand the java.net package and all it's features. And to learn it, I'm experiementing with a networking program in it's simplest form.
The 3 methods I am experimenting with at the moment to see exactly how it works, are: .getHostByName, .isReachable, .getCanonicalHostName.
So, I try to type lines similar to this embedded in case statements inside a switch:
import java.util.Scanner;
import java.net.*;
class Net {
public static void main(String[] args) {
Scanner selectNum = new Scanner(System.in);
int numSelect = selectNum.nextInt();
switch (numSelect) {
case 1:
InetAddress myAddr = InetAddress.getHostByName("yahoo.com");
break;
case 2:
InetAddress thisAddr = InetAddress.getHostByName("yahoo.com").isReachable(null, 100);
break;
case 3:
InetAddress thatAddr = InetAddress.getHostByName("yahoo.com").getCanonicalHostName("google.com");
break;
default:
System.out.println("Number was not 1 through 3");
break;
}
}
}
Something like that. I'm not writing this specifcally for any intended purpose other than to understand different parts ot he java.net package, and apply it in real time situations to fully demonstrate it.
So you say, this is Java elemtary programming, well that's fine, but that doesn't help me to the least. I am fully aware that the .isReachable will return a boolean, however how null and an int returns that boolean, is where is am confused.
Now, getHostByName, I have no problems with. It is self explanatory. But how an integer returns a boolean is where I get lost. I keep getting these compile errors when I try .getCanonicalHostName. It says it's a string, so I would assume the string would apply to whatever the user would enter in. Yet I keep getting message:
"InetAddress cannot be converted to String".
So, I'm sorry if I cannot uphold to your "elementary" standards, I guess I should be moved back to pre-school as far as you concerned, but I just need some clarification on this. So break it down for me, pre-school, give me an example, of the methods in action so I can apply them and comprehend them.
I am not new to programming, however I'll be the first to admit, I'm still what you could consider the Java "noob". I'm still learning the specifics, and it's the java.net package that I have initerest in learning the most.
I know you're probably an expert Java coder, and that's all and well, but help me understand this, so I can get my way down that path. We all started somewhere, so help me get there, don't just lash at me because I don't know as much as you or God. If you were to ask me how to do this same thing in PHP or C/C++, I could tell you in a heartbeat. I'm just learning how to do the same thing in Java as I can in other languages.
Thank you.
# 10
> InetAddress myAddr =
> InetAddress.getHostByName("yahoo.com");
Correct. The getHostbyName() method takes one String argument and it returns an InetAddress.
> InetAddress thisAddr =
> InetAddress.getHostByName("yahoo.com").isReachable(nu
> l, 100);
Incorrect. The isReachable() method doesn't take two arguments and it doesn't return an InetAddress. It takes one or three arguments and it returns a boolean.
> InetAddress thatAddr =
> InetAddress.getHostByName("yahoo.com").getCanonicalHostName("google.com");
Incorrect. The getCanonicalHostName() method doesn't take a String argument and doesn't return an InetAddress. It takes zero arguments and returns a String.
> I am fully aware that the .isReachable will return a
> boolean,
So why are you trying to store it into an InetAddress?
> But how an integer returns a
> boolean is where I get lost.
This statement is meaningless; that's why you're getting lost. An integer doesn't return a boolean. Passing an integer to a method which takes an integer argument and returns a boolean result, that's what returns a boolean here. That boolean has to be stored in a boolean variable. And that's what you're not doing. You're trying to store it into an InetAddress.
boolean isReachable = InetAddress.getByName("blah.com").isReachable(0);
Once you understand why that's right and what you wrote is wrong, how to fix your .getCanonicalHostName() call should be obvious.
> don't just lash at
> me because I don't know as much as you or God. If you
> were to ask me how to do this same thing in PHP or
> C/C++, I could tell you in a heartbeat.
Nobody's 'lashing out' at you. There's a long-established principle on these forums to provide explanations, not just corrected source code. Instead of accepting the explanations and trying to apply them, you've just reiterated your flawed misunderstanding, which is what caused the problem in the first place, and your mostly unchanged code too. This doesn't get you anywhere.
But you are overstating your prior programming skills considerably if you can't sort out this little problem. It's not even a Java problem, the issues are the same in any strongly typed language, C++ included. Only the syntax and the actual API are Java-specific.
ejpa at 2007-7-8 23:41:14 >

# 11
Ok, thank you ejp.
I know it's my misunderstanding that is the whole issue.
My intent is not frustration, and I am trying to explain everything to the fullest. And I know you have been programming in general longer that I have even touched code. And that's cool, I respect that.
I'm trying to soak all this in the best I can. My problem is, I have a kinesthetic learning style. So I can read something over and over and over, and sometimes not get it, so I have to read, then write it, and practice writing it, until I understand it, which is usually a short amount of time.
I mean, Java like any other programming shares the same logic;
variables, functions, operators, assignments, and math.
The onlything different as far as I'm concerned is functions are not called functions in Java, rather called a method. And that's cool with me.
It's just the logic of how the syntax is worked that's all. Thank you again. Your suggestion did work for isReachable, but didn't work for .getCanonicalHostName. I'll play around with it tomorrow, and let you know.
Thanks.
# 12
> Your suggestion did work for isReachable, but didn't work for
> .getCanonicalHostName.
I didn't make any suggestion for .getCanonicalHostName()!
I left that as an exercise for the reader: what did I change for isReachable() and why, and what do you have to change for .getCanonicalHostName() and why?
Nobody said they were the same changes ...
BTW member functions are called methods in C++ too, and any other class-based language.
And my skill or lack of it is irrelevant, as is your learning style. Calling methods correctly, and discovering what that means from their documentation, without having to ask six times on a forum somewhere, is a skill you need if you want to program, whether in Java or any other language.
ejpa at 2007-7-8 23:41:14 >

# 13
It's not worth the fuss, any fuss.
Try this code:
import java.net.*;
import java.util.Scanner;
class NetTest {
public static void main(String[] args) {
Scanner netScan = new Scanner(System.in);
String myNet = netScan.next();
try { // print 'true' or 'false'
System.out.println
(InetAddress.getByName(myNet).isReachable(null, 0, 100));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
hiwaa at 2007-7-8 23:41:14 >

# 14
try doing one thing at a time, and by the way, thanks for making my day, all the guy at work have been reading this thread and we found you very funny to say the least. But I do believe everyone can learn. So here's the code in a new way that should help even a blind monkey to understand why the line "InetAddress myAddr = InetAddress.getByName(myNet).isReachable(null, 0, 100);" is so deeply and annoyingly wrong!
Two steps:
InetAddress myAddr = InetAddress.getByName(myNet);//correct
boolean result = myAddr.isReachable(null, 0, 100);//correct
if you want to combine the two lines :
boolean result =InetAddress.getByName(myNet).isReachable(null, 0, 100); //correct
That's it.
you see before anything is assign to the left part of the = ... the whole right part is evaluated. so "InetAddress.getByName(myNet).isReachable(null, 0, 100);" return a boolean... then it tried to assigned it to the left part of the =...
if you have a boolean on the right part of = why would you put that in anything else than a boolean type on the left?. Compiler errors aren't there for discussion or arguing. It's like asking why "hello" is spell that way. It just the way it is.
you should have figure it by yourself now or I suggest that you find yourself a new line of work far from those pesky computers....
good luck
