• Advertisement
Sign in to follow this  

How to know whether I'm connected to a network or not?

This topic is 2147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
How do I know whether I'm connected to a LAN or the internet, or not connected at all?[/quote]

Depends on the purpose of application since design of internet makes such question impossible to answer reliably.

For LAN, one can check the IP. If it's 10.0.x.x or 192.168.x.x, then machine is likely on LAN. Query like this cannot determine the actual connection topology, just about all machines are on some sort of LAN (as opposed to being directly on internet backbone), so it depends on why query like that is used.
For a rough estimate of WAN connectivity, try to download something from a DNS-resolved location (it's what Windows does). While it doesn't guarantee full internet access, one can say that some WAN addresses are resolvable and routable through current interface. Ideally, you'd query some resource you own, such as your own server since that's all that matters to an application, even if the rest is non-routable or non-resolvable.

External-facing IP can be determined through a third-party service which replies with IP from which it received a request.

then which IP (among host.AddressList) shall I populate? Which index to use?[/quote]

Again, depends on purpose. Trying to determine server's external IP this way is of limited use.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement