Checkin for internet connection availabillity

  • Thread starter Finn Stampe Mikkelsen
  • Start date
F

Finn Stampe Mikkelsen

Hi

I'm currently working with an app, that needs to be Internet aware and turn
on/off functionallity depending on the availabillity of an internet
connection...

I'm currentliy using this code:

//Creating the extern function...
[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 ) ;
}

And it seems to work okay. There is however a considerable delay between
deactivating my network and the app responding to it. I'm using a timer
tick, running ticks at 100ms intervals, but it takes up to 4-5 seconds
before my app responds to the status change.. Sometimes even longer..

Is there a better/faster way to determine internet availabillity than the
above code...??

/Finn

--
Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
nicht tuhen.
 
G

Gregory A. Beamer

Is there a better/faster way to determine internet availabillity than
the above code...??

I have seen people use:

using System.Net;

IPHostEntry entry = Dns.GetHostByName("www.somesite.com");

I am not sure if it would be faster than the Win32 method you have
prescribed in your post, however. The site in question has to exist (be
resgistered with a DNS site). It does not have to be up, however, so if
you use your own site to test, it could say "connected" even if your
site/web service was down. Bear that in mind.

A variation would be something like:

using System.Net.Sockets;
....
TcpClient client = new TcpClient("www.somesite.com",80);

One option, to reduce appearances of slowness, would be one of the two
options:

1. Provide a splash screen for the app, while you are checking internet.
2. Use another thread for the check.

What you use depends on the app. The splash screen works in all
instances. The thread option can be problematic if the user can get to
the "unavailable" options prior to you disabling or hiding them. This is
not a technical issue as much as a timing issue.

When it comes down to it, the slowness of checking connection, esp. when
there is none, is an issue with latency and timeouts.

Peace and Grace,


--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

Finn said:
[...]
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{

int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}

And it seems to work okay. There is however a considerable delay between
deactivating my network and the app responding to it. I'm using a timer
tick, running ticks at 100ms intervals, but it takes up to 4-5 seconds
before my app responds to the status change.. Sometimes even longer..

Is there a better/faster way to determine internet availabillity than
the above code...??

I'm not even sure the InternetGetConnectedState() function gives you the
information you want. The docs promise only that it returns true if
there's a dial-up connection or a LAN connection. The name is
misleading, because it's part of wininet.dll and so gets "Internet" in
the name; it's not literally getting your "Internet connected state".

In general, an application should not care at all whether it's connected
to the Internet. It should care whether it can reach whatever remote
host it's intended to connect to. And the best (and only!) way for it
to do that is to try to connect to that host.

Your application should provide the Internet-specific functionality at
all times, and simply report an error to the user if it fails to
accomplish whatever operation you intend for it accomplish. You have to
do this anyway, since simply being connected to the Internet is
insufficient to guarantee success. It's almost certainly a waste of
your time and effort to try to do more.

Pete
 
F

Finn Stampe Mikkelsen

Peter Duniho said:
Finn said:
[...]
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{

int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}

And it seems to work okay. There is however a considerable delay between
deactivating my network and the app responding to it. I'm using a timer
tick, running ticks at 100ms intervals, but it takes up to 4-5 seconds
before my app responds to the status change.. Sometimes even longer..

Is there a better/faster way to determine internet availabillity than the
above code...??

I'm not even sure the InternetGetConnectedState() function gives you the
information you want. The docs promise only that it returns true if
there's a dial-up connection or a LAN connection. The name is misleading,
because it's part of wininet.dll and so gets "Internet" in the name; it's
not literally getting your "Internet connected state".

In general, an application should not care at all whether it's connected
to the Internet. It should care whether it can reach whatever remote host
it's intended to connect to. And the best (and only!) way for it to do
that is to try to connect to that host.

Your application should provide the Internet-specific functionality at all
times, and simply report an error to the user if it fails to accomplish
whatever operation you intend for it accomplish. You have to do this
anyway, since simply being connected to the Internet is insufficient to
guarantee success. It's almost certainly a waste of your time and effort
to try to do more.

Pete

Hi Pete

I see your point.. Will consider it again and either check the availabillity
of the specific service or simply report error reaching the service..

Thanks..

/Finn
--
Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
nicht tuhen.
 
N

News123

Well, what would be a good way to start enable / disable dial up
connection whenever a ethernet connection is obtained / lost?


The idea is, that the dial up connection should only be a emergency
solution in case the eth connection is dead.


thanks for any suggestions


N
Peter Duniho said:
Finn said:
[...]
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{

int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}

And it seems to work okay. There is however a considerable delay
between deactivating my network and the app responding to it. I'm
using a timer tick, running ticks at 100ms intervals, but it takes up
to 4-5 seconds before my app responds to the status change..
Sometimes even longer..

Is there a better/faster way to determine internet availabillity than
the above code...??

I'm not even sure the InternetGetConnectedState() function gives you
the information you want. The docs promise only that it returns true
if there's a dial-up connection or a LAN connection. The name is
misleading, because it's part of wininet.dll and so gets "Internet" in
the name; it's not literally getting your "Internet connected state".

In general, an application should not care at all whether it's
connected to the Internet. It should care whether it can reach
whatever remote host it's intended to connect to. And the best (and
only!) way for it to do that is to try to connect to that host.

Your application should provide the Internet-specific functionality at
all times, and simply report an error to the user if it fails to
accomplish whatever operation you intend for it accomplish. You have
to do this anyway, since simply being connected to the Internet is
insufficient to guarantee success. It's almost certainly a waste of
your time and effort to try to do more.

Pete

Hi Pete

I see your point.. Will consider it again and either check the
availabillity of the specific service or simply report error reaching
the service..

Thanks..

/Finn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top