How to check whether internet connection is avaliable or not from Swing

Hii JavatiesI am developing a application in Swing .I need to call a servlet from Swing.So how can i check tht internet connection is avaliable or not. ?
[181 byte] By [help_eachothera] at [2007-10-2 21:36:15]
# 1
One way is to try opening a Socket to a well known Host Name/IP address. If you connect, you know you have outbound connectivity.
Jasprea at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 2
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.
ejpa at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 3
Got the point.
help_eachothera at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 4

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

Jasprea at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 5

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.

ejpa at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 6

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

Jasprea at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 7

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.

ejpa at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 8
In hindsight it would have been prudent for me to qualify my response with "It's best to do diagnostic logic AFTER you have unsuccessfully attempted to connect to your servlet". You are right that it's a waste of time to do it prior to failure.
Jasprea at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 9

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

basharbda at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 10
NoRouteToHostException only happens in response to the initial connect. If the connection already exists and then the network goes down your only defence is a read timeout.
ejpa at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 11
so how can i handle that? i mean i need to catch that. is there any way to do?thanksbashar
basharbda at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 12
Yes, there's a setReadTimeout in the API's you're using, have a look.
ejpa at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 13
hello ejpthanks. but i have a argument - why from some PCs it is working? i have tested 4 PCs. among those 2 works fine and 2 are not.anyway you specified 'setReadTimeout' but it is not clear to me. will you please give some details?thank you
basharbda at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 14
(a) who said it was always going to work from every PC? Who said the network in between is perfect? You have to cope with failure.(b) Set a read timeout using the API I named. If it goes off you will have to catch InterruptedIOException. Treat as a failure.
ejpa at 2007-7-14 0:50:25 > top of Java-index,Core,Core APIs...
# 15

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

basharbda at 2007-7-21 8:06:23 > top of Java-index,Core,Core APIs...
# 16

> 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?

ejpa at 2007-7-21 8:06:23 > top of Java-index,Core,Core APIs...