Connected to the network or not?
-
Hi all, I want to check whether computer is connected to the network or not. I use a boolean method IsNetworkConnected() as shown below. I wonder if this method is OK, if not can you suggest any method?
public bool IsNetworkConnected() { IPAddress IPAd; ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAd = ipHostInfo.AddressList[0]; if (IPAd.ToString() == "127.0.0.1") { return false; } else { return true; } }
-
Hi all, I want to check whether computer is connected to the network or not. I use a boolean method IsNetworkConnected() as shown below. I wonder if this method is OK, if not can you suggest any method?
public bool IsNetworkConnected() { IPAddress IPAd; ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAd = ipHostInfo.AddressList[0]; if (IPAd.ToString() == "127.0.0.1") { return false; } else { return true; } }
-
Hi all, I want to check whether computer is connected to the network or not. I use a boolean method IsNetworkConnected() as shown below. I wonder if this method is OK, if not can you suggest any method?
public bool IsNetworkConnected() { IPAddress IPAd; ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAd = ipHostInfo.AddressList[0]; if (IPAd.ToString() == "127.0.0.1") { return false; } else { return true; } }
It really depends on what you mean by "connected to the network". If you are only interested in determining if the network is available (ie, the cable is plugged in and the NIC is getting a signal) you can use the NetworkAvailabilityChangedEventHandler delegate[^]. Using the AddressList property as you show above isn't the most reliable way to check as it may not return the addresses in the order you are expecting, so AddressList[0] isn't guaranteed to be the loopback address. (The better way to test if it is the loopback address is also to do
if (IPAd == IPAddress.Loopback)
orif (IPAddress.IsLoopback(IPAd))
.) If you need to know if you are connected to the internet, the best options are to try to browse to a website or ping a known address. Using ping:bool internetAvailable = false;
Ping pinger = new Ping();
PingOptions pingOptions = new PingOptions(1, true);
byte[] pingResults = new byte[2];PingReply pingReply = pinger.Send("www.microsoft.com", 25000, pingResults, pingOptions);
if (pingReply.Status == IPStatus.Success)
{
internetAvailable = true;
}Using website:
bool internetAvailable = false;
Uri requestUri = new UriBuilder("http", "www.microsoft.com").Uri;
HttpWebRequest connectivityCheck = WebRequest.Create(requestUri) as HttpWebRequest;// A Domain Name System (DNS) query may take up to 15 seconds to return or time out, so
// set the timeout to 25 seconds.
connectivityCheck.Timeout = 25000;HttpWebResponse connectivityResponse = connectivityCheck.GetResponse() as HttpWebResponse;
if (connectivityResponse.StatusCode == HttpStatusCode.OK)
{
if (!connectivityResponse.IsFromCache)
{
internetAvailable = true;
}
}----------------------------- In just two days, tomorrow will be yesterday.
-
It really depends on what you mean by "connected to the network". If you are only interested in determining if the network is available (ie, the cable is plugged in and the NIC is getting a signal) you can use the NetworkAvailabilityChangedEventHandler delegate[^]. Using the AddressList property as you show above isn't the most reliable way to check as it may not return the addresses in the order you are expecting, so AddressList[0] isn't guaranteed to be the loopback address. (The better way to test if it is the loopback address is also to do
if (IPAd == IPAddress.Loopback)
orif (IPAddress.IsLoopback(IPAd))
.) If you need to know if you are connected to the internet, the best options are to try to browse to a website or ping a known address. Using ping:bool internetAvailable = false;
Ping pinger = new Ping();
PingOptions pingOptions = new PingOptions(1, true);
byte[] pingResults = new byte[2];PingReply pingReply = pinger.Send("www.microsoft.com", 25000, pingResults, pingOptions);
if (pingReply.Status == IPStatus.Success)
{
internetAvailable = true;
}Using website:
bool internetAvailable = false;
Uri requestUri = new UriBuilder("http", "www.microsoft.com").Uri;
HttpWebRequest connectivityCheck = WebRequest.Create(requestUri) as HttpWebRequest;// A Domain Name System (DNS) query may take up to 15 seconds to return or time out, so
// set the timeout to 25 seconds.
connectivityCheck.Timeout = 25000;HttpWebResponse connectivityResponse = connectivityCheck.GetResponse() as HttpWebResponse;
if (connectivityResponse.StatusCode == HttpStatusCode.OK)
{
if (!connectivityResponse.IsFromCache)
{
internetAvailable = true;
}
}----------------------------- In just two days, tomorrow will be yesterday.