By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

Determining if a client PC has an Internet connection

P: n/a
Hi,

I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

I've considered trying to connect to a stable host (i.e. Google) and if
that fails then assume the client can't connect to the internet, but
this seems like a sub-optimal solution.

Is there any way to reliably determine the state of the client's
internet connectivity?

Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>

Jul 18 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Cliff Wells <cl************@comcast.net> pisze:
I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

I've considered trying to connect to a stable host (i.e. Google) and if
that fails then assume the client can't connect to the internet, but
this seems like a sub-optimal solution.

Is there any way to reliably determine the state of the client's
internet connectivity?


No, there's no such way. You can try to connect to some "usually
working & available" host, but there always may be some periods, when
this host is not available (scheduled downtime, power outage, you name
it). The hosts known for its greatest stability are root DNS servers.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #2

P: n/a
* Jarek Zgoda (2004-09-19 10:14 +0200)
Cliff Wells <cl************@comcast.net> pisze:
I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

I've considered trying to connect to a stable host (i.e. Google) and if
that fails then assume the client can't connect to the internet, but
this seems like a sub-optimal solution.

Is there any way to reliably determine the state of the client's
internet connectivity?


No, there's no such way.


I think there has to be (or at least there is a method that many
coders use because "testing if you're online" is a very frequent
problem).

Thorsten
Jul 18 '05 #3

P: n/a
Thorsten Kampe <th******@thorstenkampe.de> pisze:
Is there any way to reliably determine the state of the client's
internet connectivity?


No, there's no such way.


I think there has to be (or at least there is a method that many
coders use because "testing if you're online" is a very frequent
problem).


There has to be, but isn't. I often see this question on many forums
with single answer. Let me cite ICS FAQ:

"""
There is no perfect solution. Why? Because the Internet is just another
network. Your computer does not distinguish between LAN and Internet.
They are both networks. One of them just happens to be very big.

The Internet is no different to your local network. It is just a matter
of size.

Think about your question. "Am I connected to the Internet?". Try
rephrasing your question to what you really want to ask. "Can I connect
to a specific remote host?" With the question worded like this, you can
start to tackle the problem. How do you find out if you can connect to
the remote host? Try to connect. Simple as that. If the connection
attempt fails, there is no path between you and the remote host or the
remote host is refusing connections.

But, I hear you cry, I do not want the auto-dial the pop up. How do we
get around this? Ask the user how to make a connection. We see this all
the time in other Internet applications. Outlook Express, WinAmp, etc.
They ask the user what sort of network connection they have. From this
information, the applications can make the best choice about how to
connect to a remote host.

There is no proof way. However some things can help.

There is an InternetGetConnectedState function in the WinINet.DLL that
returns true if it detects a connection to the internet. However, the
only thing you can be sure about when using this funtion is that the
computer will not start dialing and not pop up any dialog box when the
function returned true and you afterwards try to do anything on the
internet. In some cases the function may return false although the
computer is connected.
"""

Although it is Win32-only.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #4

P: n/a
Cliff Wells wrote:
Hi,

I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).

The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.

I've considered trying to connect to a stable host (i.e. Google) and if
that fails then assume the client can't connect to the internet, but
this seems like a sub-optimal solution.

Is there any way to reliably determine the state of the client's
internet connectivity?

Regards,
Cliff


As others have mentioned, there's no clean-cut paradigm in the language
that can distinguish between the Internet and your local LAN. Your best
bet is to simple go about your business can try accessing hosts like
normal. When several of your requests timeout, then you can safely
assume the local host to be offline. That's what you see in those
applications when they say you're "offline"; an educated guess. After
all, if you can't reach your target, then you are, for all intents and
purposes, offline.
Jul 18 '05 #5

P: n/a
On Sun, 2004-09-19 at 09:03 +0000, Chris S. wrote:

As others have mentioned, there's no clean-cut paradigm in the language
that can distinguish between the Internet and your local LAN. Your best
bet is to simple go about your business can try accessing hosts like
normal. When several of your requests timeout, then you can safely
assume the local host to be offline. That's what you see in those
applications when they say you're "offline"; an educated guess. After
all, if you can't reach your target, then you are, for all intents and
purposes, offline.


Okay, at this point I suppose the better question would be how to make
an "educated guess". As I mentioned, I've considered trying to open a
socket to one or more relatively stable hosts (i.e. Google, Yahoo, etc)
and if this fails, assume there is no Internet link. However, this
clearly has it's flaws (those services, as unlikely as it seems, could
potentially be down, or they might be blocked by a corporate firewall,
etc). Further, tying the application to an outside source that is, for
all intents, unrelated to the app seems a bit flaky.

Anyway, it occurs to me that a better "guess" might consist of whether
or not the app can reach the PC's primary/secondary DNS servers. If
these are unreachable then it's a fair assumption that we aren't going
to get anywhere anyhow.

So... is there a way of determining the DNS search path of a PC?

Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>

Jul 18 '05 #6

P: n/a
Hi !

Not only Internet, but with good suspicion : test if IP-passerelle is
present and answer to ping.

Then Ping one or two IP "externals and stables".


Jul 18 '05 #7

P: n/a
On Sun, 2004-09-19 at 13:31 +0200, Michel Claveau - abstraction méta-
galactique non triviale en fuite perpétuelle. wrote:
Hi !

Not only Internet, but with good suspicion : test if IP-passerelle is
present and answer to ping.
Sorry, you lost me here: passerelle == bridge?
Then Ping one or two IP "externals and stables".


Again, I don't follow.

Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>

Jul 18 '05 #8

P: n/a
Cliff Wells wrote:
I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).
I do not see why there would have to be specific online and offline
modes. They are annoying. Especially when they educatedly guess they are
not on the Internet and refuse to believe the user who says they are.
The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.


So why not assume you are always connected to the Internet. Download
updated info into local cache whenever possible and always show local
cached information. Of course you could somehow indicate the age of the
cached information, so the user doesn't think that local cached
information is up-to-date when it actually isn't.
Jul 18 '05 #9

P: n/a
In article <ma**************************************@python.o rg>,
Cliff Wells <cl************@comcast.net> wrote:
On Sun, 2004-09-19 at 09:03 +0000, Chris S. wrote:

Jul 18 '05 #10

P: n/a
On Sun, 2004-09-19 at 23:54 +0300, Tuure Laurinolli wrote:
Cliff Wells wrote:
I'm writing an application that needs to know if an Internet connection
is available. Basically, I want to have something similar to what a lot
of email clients have, where the app can work either in "online" or
"offline" mode (it keeps a cache of downloaded info, so it can work
without a connection if needed).


I do not see why there would have to be specific online and offline
modes. They are annoying. Especially when they educatedly guess they are
not on the Internet and refuse to believe the user who says they are.


It doesn't refuse. If you tell it that it's online it will faithfully
try to download whether it thinks it is or not. You are making as many
assumptions as you claim my software will ;)
The basic problem is this: it downloads info (RSS feeds) from a variety
of sources. Any one (or more) of these could conceivably fail to
download, so simply waiting for a timeout isn't sufficient (not easy to
differentiate between having a bad server and the client not having a
connection). Further, if it waits (say 30s) for the timeout to occur,
this is going to be a bit annoying to the user.


So why not assume you are always connected to the Internet. Download
updated info into local cache whenever possible and always show local
cached information. Of course you could somehow indicate the age of the
cached information, so the user doesn't think that local cached
information is up-to-date when it actually isn't.


Because it's a customer requirement. Besides, as I outlined in a
separate post, there are actually a couple of good reasons for the app
to know when it's offline:

'''
More importantly, if the app is "online", timing out is returned as an
error to the user, whereas in the "offline" state, as long as the data
is cached, there is no error. Also, in the case of being offline and
there is no data in the cache, the error message is to be different than
if the app is online and the connection fails.
'''

Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>

Jul 18 '05 #11

P: n/a
Cliff Wells wrote:
On Sun, 2004-09-19 at 09:03 +0000, Chris S. wrote:

As others have mentioned, there's no clean-cut paradigm in the language
that can distinguish between the Internet and your local LAN. Your best
bet is to simple go about your business can try accessing hosts like
normal. When several of your requests timeout, then you can safely
assume the local host to be offline. That's what you see in those
applications when they say you're "offline"; an educated guess. After
all, if you can't reach your target, then you are, for all intents and
purposes, offline.

Okay, at this point I suppose the better question would be how to make
an "educated guess". As I mentioned, I've considered trying to open a
socket to one or more relatively stable hosts (i.e. Google, Yahoo, etc)
and if this fails, assume there is no Internet link. However, this
clearly has it's flaws (those services, as unlikely as it seems, could
potentially be down, or they might be blocked by a corporate firewall,
etc). Further, tying the application to an outside source that is, for
all intents, unrelated to the app seems a bit flaky.

Anyway, it occurs to me that a better "guess" might consist of whether
or not the app can reach the PC's primary/secondary DNS servers. If
these are unreachable then it's a fair assumption that we aren't going
to get anywhere anyhow.

So... is there a way of determining the DNS search path of a PC?


My solution was far more mundane. I meant simply try and access the
internet the way you normally would. If it's a mail client, try and
access your users POP server. If it's a web-browser, try and retrieve a
web page the user wants. If you can't access them, then there's
essentially no "internet connection". In other words, don't worry about
whether or not there's an available connection. Instead, assume there is
a connection and worry about the status of your requests on the network.
Jul 18 '05 #12

P: n/a
On Sun, 2004-09-19 at 23:38 +0000, Chris S. wrote:
Cliff Wells wrote:

Okay, at this point I suppose the better question would be how to make
an "educated guess". As I mentioned, I've considered trying to open a
socket to one or more relatively stable hosts (i.e. Google, Yahoo, etc)
and if this fails, assume there is no Internet link. However, this
clearly has it's flaws (those services, as unlikely as it seems, could
potentially be down, or they might be blocked by a corporate firewall,
etc). Further, tying the application to an outside source that is, for
all intents, unrelated to the app seems a bit flaky.
>
Anyway, it occurs to me that a better "guess" might consist of whether
or not the app can reach the PC's primary/secondary DNS servers. If
these are unreachable then it's a fair assumption that we aren't going
to get anywhere anyhow.

So... is there a way of determining the DNS search path of a PC?


My solution was far more mundane. I meant simply try and access the
internet the way you normally would. If it's a mail client, try and
access your users POP server. If it's a web-browser, try and retrieve a
web page the user wants. If you can't access them, then there's
essentially no "internet connection". In other words, don't worry about
whether or not there's an available connection. Instead, assume there is
a connection and worry about the status of your requests on the network.


Well, this is the fundamental problem. It's an RSS aggregator, so there
is no single server it will be connecting to. Further, because servers
can be added by the user or simply "disappear" because some blogger
decides he doesn't want to maintain a feed anymore, getting connection
errors will probably be quite common.

Additionally, because I'm using Twisted, fetching the sites is
asynchronous, so I'm updating the cache and determining error messages
long before I have a chance to determine that all of the connections are
failing. By the time I have that information, the damage has been done,
so to speak.

Given this situation, and the fact that there are specific requirements
(mentioned elsewhere) leaves me little choice but to use some guesswork
to determine whether or not a connection is present *prior* to fetching
feeds.

Anyway, at this point I'm using the rather lame "check if both google
and yahoo are unavailable" method. I'll find something better when I
have more time to devote to it. I may use the effbot's suggestion for
Windows (InternetCheckConnection) and some other method for Linux and
OS/X.

Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>

Jul 18 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.