467,074 Members | 1,085 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

how to determine Internet connection state.

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

--
Ron Vecchi

Nov 15 '05 #1
  • viewed: 5472
Share:
2 Replies
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
}

On Fri, 5 Dec 2003 07:45:12 -0500, "Ron Vecchi"
<rv*****@xilehdvecchi.com> wrote:
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


Nov 15 '05 #2
Thanks for the info, Helped a lot!

<om***@yahoo.com> wrote in message
news:26********************************@4ax.com...
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
}

On Fri, 5 Dec 2003 07:45:12 -0500, "Ron Vecchi"
<rv*****@xilehdvecchi.com> wrote:
I have an aplication that will consitently be comutication with a webserverremotly 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

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Adam Parkin | last post: by
12 posts views Thread by Cliff Wells | last post: by
3 posts views Thread by Jonny | last post: by
8 posts views Thread by Steven Van Dyke | last post: by
7 posts views Thread by semedao | last post: by
1 post views Thread by ricolee99@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.