views:

55

answers:

3

I did a quick search on how to check whether Internet is available or not. Most of them talked about making InterOp calls to wininet.dll.

One of the answers pointed towards System.Net.NetworkInformation namespace. Exploring the namespace I found a class Ping which could be used to pinging to our servers from code, and checking whether server is available or not.

What I want to ask is how this solution compares to other solutions?

Ping soPing = new Ping();
var soPingReply = soPing.Send("www.stackoverflow.com");
if (soPingReply.Status != IPStatus.Success)
{
    // SO not available
}
+1  A: 

Try this:

[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

//Creating a function that uses the API function...
public static bool IsConnectedToInternet()
{
    int Desc;
    return InternetGetConnectedState(out Desc, 0);
}
portland
And how does this solution compare to the one that I gave?
Trainee4Life
it uses standart win32 method.The problem is that you do not need to answer a question "what does 'internet is availible' mean?" What if you 'have' internet, but so is temporary down? you have to implement more complex logic here(check several sites for ex), or rely on windows built-in tool. As for me, i'd prefer to check availiblity of the concrete site/server i need, but not the whole internet. THis information is generally more useful.(of course, not in case you are building search engine:)))
portland
"I actually wanted to check connectivity to one of the client's site. If present then load it, else load an offline page in the web browser control." As suggested by you, checking that particular site/server is always a better choice. How should I go about it?
Trainee4Life
I implemented this using simple try-catch(TimeOutException ex)Really, google the problem, there're a lot of answers in there, i don't not the best one, probably, it depends on particular situation.
portland
A: 

Network connectivity has nothing to do with the ability to ping some (random?) site. If you're behind a firewall or the destination is that ping (or the response) may well be blocked when actual traffic isn't.

And even if that ping works, there's no guarantee that connection will still be up by the time you want to make the actual request. So best to just do the actual request and make sure you handle any network errors that may occur correctly.

jwenting
I actually wanted to check connectivity to one of the client's site. If present then load it, else load an offline page in the web browser control.
Trainee4Life
same thing. Just try to load it. If it fails, show the offline page, if it doesn't, the real one.
jwenting
A: 

How long does it try and ping for? Does it matter if your app locks up for 30secs while it does so?

Do you have an internet connection but it's temporarily down?

Is it the internet that's down or is the SO site?

What if the local firewall blocks ping?

Martin Beckett
The IPStatus enum has quite a lot of Status fields. May be it will return something other than success for cases that you mentioned. Also while pinging, one can also specify a timeout.
Trainee4Life