Test for network connection

R

Rich

I aplogize for posting on a topic which I'm sure has already been
discussed here, but I have searched the archives and not found a
solution that works quite like I was hoping.

I'm developing a Compact Framework application which needs to work in
both "connected" and "disconnected" modes. The user has an option to
synchronize the locally saved data with the master SQL database when
necessary, and I would like to test for an active network connection
prior to doing so. I have expirmented with the WebRequest class and it
works, but it seems especially slow to return an HttpStatusCode.

It just seems that there should be a simpler, quicker way to get this
status. Does anyone have any suggestions or advice?

Thanks in advance.
 
S

Sergey Bogdanov

Here is a code snippet:

static public bool IsConnected
{
get
{
bool ret;
try
{
string HostName = Dns.GetHostName();
IPHostEntry thisHost = Dns.GetHostByName(HostName);
string thisIpAddr = thisHost.AddressList[0].ToString();

ret = thisIpAddr != IPAddress.Parse("127.0.0.1").ToString();
}
catch
{
return false;
}

return ret;
}
}


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
R

Rich

This technique seems to work well for detecting whether an active
connection has been lost. However, I'm having problems using this
technique on a device which has just been booted up and no connection
is available. The code just hangs for several seconds and an exception
is eventually thrown. Any ideas?

Thanks.

Sergey said:
Here is a code snippet:

static public bool IsConnected
{
get
{
bool ret;
try
{
string HostName = Dns.GetHostName();
IPHostEntry thisHost = Dns.GetHostByName(HostName);
string thisIpAddr = thisHost.AddressList[0].ToString();

ret = thisIpAddr != IPAddress.Parse("127.0.0.1").ToString();
}
catch
{
return false;
}

return ret;
}
}


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
I aplogize for posting on a topic which I'm sure has already been
discussed here, but I have searched the archives and not found a
solution that works quite like I was hoping.

I'm developing a Compact Framework application which needs to work in
both "connected" and "disconnected" modes. The user has an option to
synchronize the locally saved data with the master SQL database when
necessary, and I would like to test for an active network connection
prior to doing so. I have expirmented with the WebRequest class and it
works, but it seems especially slow to return an HttpStatusCode.

It just seems that there should be a simpler, quicker way to get this
status. Does anyone have any suggestions or advice?

Thanks in advance.
 
S

Sergey Bogdanov

If exception was thrown then IsConnected returns false. That is why the
main part of the property included into try ... catch block.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com

This technique seems to work well for detecting whether an active
connection has been lost. However, I'm having problems using this
technique on a device which has just been booted up and no connection
is available. The code just hangs for several seconds and an exception
is eventually thrown. Any ideas?

Thanks.

Sergey said:
Here is a code snippet:

static public bool IsConnected
{
get
{
bool ret;
try
{
string HostName = Dns.GetHostName();
IPHostEntry thisHost = Dns.GetHostByName(HostName);
string thisIpAddr = thisHost.AddressList[0].ToString();

ret = thisIpAddr != IPAddress.Parse("127.0.0.1").ToString();
}
catch
{
return false;
}

return ret;
}
}


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
I aplogize for posting on a topic which I'm sure has already been
discussed here, but I have searched the archives and not found a
solution that works quite like I was hoping.

I'm developing a Compact Framework application which needs to work
in
both "connected" and "disconnected" modes. The user has an option
to
synchronize the locally saved data with the master SQL database
when
necessary, and I would like to test for an active network
connection
prior to doing so. I have expirmented with the WebRequest class

and it
 

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

Top