> Just call the servlet and see why it failed if it
> fails. Don't try to second-guess or do something
> prior to the servlet call, it's pointless.
I wouldn't say it's pointless. Java doesn't exactly have very informative exception messages when network problems occur. If the connection to the servlet fails, you may not know if the problem is on the client side or server side. One way to check for an outbound connection is to access a well-known HTTP server using URL/URLConnection. This way, a client GUI can inform the user (perhaps running dial-up) that he needs to open an Internet connection first.
As a matter of fact Java's exceptions map very precisely to the error codes available at the C level from the TCP/IP stack. They can't do any more.
Being able to connect to Yahoo or Google doesn't really help much if you can't connect to what you want to connect to. And if you can't connect to Yahoo/Google/whatever you still get the same non-'informative' exceptions.
If a dial-up isn't connected the exception will be NoRouteToHostException.
[sigh] I'm not arguing that Java has a problem or deficiency, I'm saying the error messages aren't extremely helpful in diagnosing network problems. You seem to be claiming that one error is better than two, but when the user calls customer support because your application isn't working, what are you going to tell them - "sorry, we couldn't connect to our servlet, but we know kind of what the problem might be"? So, what happens when you see the NoRouteToHostException or the generic SocketException with some preformatted error message that you can't customize because Java didn't give you the error code? Do you assume that it means "no internet connection", or do you try some other things to diagnose the problem programmatically before telling the user they're screwed? IMO, once you've tripped over a network error, the more diagnostics you do on the problem before giving up, the better.
I think we're probably in a state of furious agreement here. What I'm trying to argue against, possibly irrelevantly, is the practice of pinging the target host prior to trying the connection. It just leaves you with a timing-window error and two sites for handling the same set of exceptions.
i am also fatching this type of problem. my applet is playing a audio file from server. during the playing time if network connection failed then NoRouteToHostException occurs from some PCs. but there are some PCs it doesn't occur though the playing is stoped.
all PCs OS windows XP and jre jdk1.5.0_06
i can't find out the problem. can any one help me for this?
code:
URL testURL = null;
try {
testURL = new URL(sourcePath);
//inputStream = testURL.openStream();
URLConnection connection = testURL.openConnection();
connection.connect();
} catch(NoRouteToHostException e) {
System.out.println("NoRouteToHostException block: " + e.getMessage());
........................
}
thanks
bashar
dear sir
sorry i can't example my problem. if network connection goes down then the specified code should throw the NoRouteToHostException. i have a class PlaySound and using this class i am playing audio file and i am checking the connection during playing. so it checks the connection is OK or not during palying. if connection failed then it should gives NoRouteToHostException. i have check 4 PCs - i mean during playing i have disconnect my network and from 2 PCs my program can catch the NoRouteToHostException and others can't. so i need to know is it possible to check network connection using URLConnection? or i need to use SocketServer for this.
thanks
bashar
> if network connection goes down then the specified code should
> throw the NoRouteToHostException.
Nope, already answered that: you are mistaken about this.
> i have a class PlaySound and using this class i am playing
> audio file and i am checking the connection during
> playing. so it checks the connection is OK or not
> during palying.
How do you think you are going to do this? There is no Java API and there is no technique at the C level or the TCP/IP protocol layer either.
> if connection failed then it should
> gives NoRouteToHostException.
Nope, already answered that: you are mistaken about this.
> i have check 4 PCs - i mean during playing i have disconnect my
> network and from 2 PCs my program can catch the
> NoRouteToHostException and others can't. so i
> need to know is it possible to check network
> connection using URLConnection? or i need to use
> SocketServer for this.
I don't know why you would think either of these will do what you want. I also don't know why people ask for help if they're not prepared to use it. Why don't you stop guessing; and why don't you try the read timeout technique I suggested?