how to determine Internet connection state.

  • Thread starter Thread starter Ron Vecchi
  • Start date Start date
R

Ron Vecchi

I have an aplication that will consitently be comutication with a webserver
remotly via Internet. What is the best way and most lightweight way to
constently check the conection so my app will know immediatly when
connection is lost or connection is regained?


Thanks
 
The simplest way I've found is to import the
InternetGetConnectedState() from the wininet.dll.

You can do this by declaring a function as such:

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

be sure you're using the
System.Runtime.InteropServices namespace as well.

Then you can spawn a new thread to constantly be checking the internet
state by using:

int desc = 0;
if(InternetGetConnectedState(out desc, 0 /* must be zero */))
{
// we have an internet connection
}

If you need more information you can bitwise and the value of desc
with the following enum.

public enum ConnectedStates
{
INTERNET_CONNECTION_MODEM = 1, INTERNET_CONNECTION_LAN = 2,
INTERNET_CONNECTION_PROXY = 4,
INTERNET_CONNECTION_MODEM_BUSY = 8, INTERNET_RAS_INSTALLED =
16, INTERNET_CONNECTION_OFFLINE = 32,
INTERNET_CONNECTION_CONFIGURED = 64,
}

So if you want to check if you're connected via the LAN, you could
make this check
if(code & ConnectedStates.INTERNET_CONNECTION_LAN ==
ConnectedStates.INTERNET_CONNECTION_LAN)
{
// connected via LAN
}
 
Thanks for the info, Helped a lot!

The simplest way I've found is to import the
InternetGetConnectedState() from the wininet.dll.

You can do this by declaring a function as such:

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

be sure you're using the
System.Runtime.InteropServices namespace as well.

Then you can spawn a new thread to constantly be checking the internet
state by using:

int desc = 0;
if(InternetGetConnectedState(out desc, 0 /* must be zero */))
{
// we have an internet connection
}

If you need more information you can bitwise and the value of desc
with the following enum.

public enum ConnectedStates
{
INTERNET_CONNECTION_MODEM = 1, INTERNET_CONNECTION_LAN = 2,
INTERNET_CONNECTION_PROXY = 4,
INTERNET_CONNECTION_MODEM_BUSY = 8, INTERNET_RAS_INSTALLED =
16, INTERNET_CONNECTION_OFFLINE = 32,
INTERNET_CONNECTION_CONFIGURED = 64,
}

So if you want to check if you're connected via the LAN, you could
make this check
if(code & ConnectedStates.INTERNET_CONNECTION_LAN ==
ConnectedStates.INTERNET_CONNECTION_LAN)
{
// connected via LAN
}

I have an aplication that will consitently be comutication with a webserver
remotly via Internet. What is the best way and most lightweight way to
constently check the conection so my app will know immediatly when
connection is lost or connection is regained?


Thanks
 
Back
Top