I have a java application which runs every 10 minutes. So whenever the thread starts executing i have to check whether there is internet connectivity or not. So i was using the Socket class for this. But the problem here is the socket class works fine if the wire is not connected to my system. For me if i plug in the wire then i have LAN but no internet. In this case the socket class fails to throw an error. Here is my code -
Socket socket = null;
try {
socket = new Socket("www.google.com", 80);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) try { socket.close();
}
catch(IOException e) {
e.printStackTrace();
}
System.exit(0);
}
I checked out the code here - Detect internet Connection using Java. But using the getContent() does not help.