patternjavaMinor
Periodically checking for internet connection
Viewed 0 times
checkinginternetperiodicallyforconnection
Problem
I have a SwingWorker which checks periodically for an internet connection and updates a jLabel accordingly.
Is there maybe a more proper way of doing it or even a library with net tools (along with database management tools or general utility tools) that could do this and have less custom code littering my project?
All in all I am looking for the most minimalistic way.
public class InternetCheckWorker extends SwingWorker {
@Override
protected Void doInBackground() throws InterruptedException {
while (true) {
try (Socket socket = new Socket(HOSTNAME, 80);) {
process("on");
} catch (IOException e) {
process("off");
}
Thread.sleep(500);
}
}
protected void process(String status) {
jInternetConnectionLabel.setText("Connection " + status);
}
}
private static final String HOSTNAME = "www.google.com";Is there maybe a more proper way of doing it or even a library with net tools (along with database management tools or general utility tools) that could do this and have less custom code littering my project?
All in all I am looking for the most minimalistic way.
Solution
I know this may sound silly but what exactly do mean by connected to the internet? You're going to have to define what this means to your application.
In your example, if "www.google.com" goes down then the user is no longer connected to the internet and this is not necessarily true. Last time I checked, google isn't the internet (although it is very close).
The problem all boils down to the fact that the user may be connected to some part of the internet but unable to reach other parts due to corporate/national firewalls, broken equipment, wrongly configured hosts files etc. Or just a very zealous ISP who insists every one go through their gateway googlez.net to show you ads on every google search.
It makes more sense to test for an URL that actually means something for your application (I can't tell what this is going to be used for so I can't offer anything more specific). Like a service that you interact with.
As you see it's a tricky question and you need to answer some of these questions before you can come up with a good solution.
In your example, if "www.google.com" goes down then the user is no longer connected to the internet and this is not necessarily true. Last time I checked, google isn't the internet (although it is very close).
The problem all boils down to the fact that the user may be connected to some part of the internet but unable to reach other parts due to corporate/national firewalls, broken equipment, wrongly configured hosts files etc. Or just a very zealous ISP who insists every one go through their gateway googlez.net to show you ads on every google search.
It makes more sense to test for an URL that actually means something for your application (I can't tell what this is going to be used for so I can't offer anything more specific). Like a service that you interact with.
As you see it's a tricky question and you need to answer some of these questions before you can come up with a good solution.
Context
StackExchange Code Review Q#59252, answer score: 5
Revisions (0)
No revisions yet.