Reputation: 4836
I am programming a Java app in which i need to constantly check that if user is connected to internet or not. I'm doing this by constantly pinging a url and reading the response. I want my app to work in following scenario Suppose when i started my app user was connected to internet but in between the internet connection is lost. I'm pining the url in a separate thread but the problem is if internet is disconnected in between the thread hangs and it doesn't gives an error that internet connection is lost. I'm using following code
p1 = java.lang.Runtime.getRuntime().exec("ping www.yahoo.com");
BufferedReader input = new BufferedReader(new InputStreamReader(p1
.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
Upvotes: 0
Views: 418
Reputation: 382122
Don't call an external program to do that :
InetAddress inet = InetAddress.getByName("www.yahoo.com");
boolean ok = inet.isReachable(timeout);
Upvotes: 4