Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Shashwat Rohilla   Members   -  Reputation: 130

Like
0Likes
Like

Posted 06 March 2012 - 03:45 AM

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.

Sponsor:

#2 Antheus   Members   -  Reputation: 2389

Like
1Likes
Like

Posted 06 March 2012 - 09:59 AM

How do I know whether I'm connected to a LAN or the internet, or not connected at all?


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?


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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS