Connecton detection

  • Thread starter Thread starter Harry Simpson
  • Start date Start date
H

Harry Simpson

I'm just issuing an HTTP request to a know web site to tell me if I have a
connection to the wap. Is there an event that I can catch in CF2 that will
fire when the WiFi/Ethernet is/isn't connected to the LAN?

TIA
Harry
 
There is no way that I know of. But a simple solution is to monitor your IP
address once it equals 127.0.0.1 you know you have no network.

Alternatively, have a look at the WiFi wrapped classes OPENNetCF provide.
 
Here is some VB code :

Public ReadOnly Property Connected() As Boolean

Get

Dim ret As Boolean

Try

' Returns the Device Name

Dim HostName As String = Net.Dns.GetHostName()

Dim thisHost As Net.IPHostEntry = Net.Dns.GetHostEntry(HostName)

Dim thisIpAddr As String = thisHost.AddressList(0).ToString

ret = thisIpAddr <> _

Net.IPAddress.Parse("127.0.0.1").ToString()

Catch ex As Exception

Return False

End Try

Return ret

End Get

End Property
 
You don't need to use Parse, you can simply say:

C#:
if (thisHost.AddressList[0].ToString() == "127.0.0.1")
return false;
else
return true;

The benefit with the above is an exception will never be generated (assuming
thisHost is in fact valid and there is at least 1 valid network).
 
That's not even needed.

if (thisHost.AddressList[0].Equals(IPAddress.LocalHost)
return false;
else
return true;


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com



Simon Hart said:
You don't need to use Parse, you can simply say:

C#:
if (thisHost.AddressList[0].ToString() == "127.0.0.1")
return false;
else
return true;

The benefit with the above is an exception will never be generated
(assuming
thisHost is in fact valid and there is at least 1 valid network).

--
Simon Hart
http://simonrhart.blogspot.com


Kay-Christian Wessel said:
Here is some VB code :

Public ReadOnly Property Connected() As Boolean

Get

Dim ret As Boolean

Try

' Returns the Device Name

Dim HostName As String = Net.Dns.GetHostName()

Dim thisHost As Net.IPHostEntry = Net.Dns.GetHostEntry(HostName)

Dim thisIpAddr As String = thisHost.AddressList(0).ToString

ret = thisIpAddr <> _

Net.IPAddress.Parse("127.0.0.1").ToString()

Catch ex As Exception

Return False

End Try

Return ret

End Get

End Property
 
Back
Top