I’m using .Net 4.0 in VS 2010.
I can retreive the IP Address of my machine as
string hostname = Dns.getHostName();
IPHostEntry host = Dns.getHostEntry(hostname);
Now host.AddressList is an array of IPAddresses.
I noticed that AddressList[0] contains nothing, AddressList[1] the loopback address. I’m not sure about other indices.
If I have created a server on one machine and it wants to populate its IP to client (may be the machine only), then which IP (among host.AddressList) shall I populate? Which index to use?
How do I know whether I’m connected to a LAN or the internet, or not connected at all?
Please clarify.
There are several ways to do that (I think you may use a combination of [2] and [3]).
Solution 1
If you include a reference to
Microsoft.VisualBasicyou can useMicrosoft.VisualBasic.Devices.Network.IsAvailableproperty to check if a network connection is available (and related events to be notified when this condition changes).Solution 2
Import the API function to check it:
Or simply use the
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()function (better solution, the only drawback is that it’s supported in the Client Profile only from 4.0).Solution 3
Ping a known host name like Google or Microsoft (this will check DNS too).
Example
Use a combination of above techniques (in this example I use the imported API but you may prefer the other one).