6
\$\begingroup\$

I have a SwingWorker which checks periodically for an internet connection and updates a jLabel accordingly.

public class InternetCheckWorker extends SwingWorker<Void, String> {

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

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I actually plan on having 2 "stages" of whether I have a desired connection or not. The first is to check as generally as possible for an internet connection (by testing google.com) and the second will be checking my database url (the specific part that you mentioned). I just want the user to be able to tell if his internet connection is the issue or if the database server has the issue. \$\endgroup\$ – Aki K Aug 6 '14 at 16:12
  • \$\begingroup\$ In that case I would just try the database first (as you're expecting it to be up most of the time) and then if that fails just report an error. Or is it your task to troubleshoot her internet connection? In any case she will most likely be able to tell if her internet connection isn't working anyway. \$\endgroup\$ – Emily L. Aug 6 '14 at 16:15
  • \$\begingroup\$ No I don't plan on providing troubleshoot for the internet connection just to make the user know what type of connection issue there is (server or internet). \$\endgroup\$ – Aki K Aug 6 '14 at 16:25
  • \$\begingroup\$ Relevant question from Super User : superuser.com/questions/769005/… . Determining the if the "internet" connection is up s not an easy task. +1 for Emily who's spot on, verify what you need. \$\endgroup\$ – Marc-Andre Aug 6 '14 at 16:58
  • \$\begingroup\$ @SilliconTouch In that case you should just catch the exception when trying to connect and provide an informative error. For example if you catch a NoRouteToHostException then you weren't able to reach the correct network. For your application this is mostly equivalent to "not being connected to the internet". If you get a ConnectException you could reach the host machine but no one responded. All of these are subclasses of SocketException. \$\endgroup\$ – Emily L. Aug 7 '14 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.