Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Connected to the network or not?

Connected to the network or not?

Scheduled Pinned Locked Moved C#
iossysadminquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mertkan65
    wrote on last edited by
    #1

    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; } }

    S S 2 Replies Last reply
    0
    • M mertkan65

      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; } }

      S Offline
      S Offline
      saqib82
      wrote on last edited by
      #2

      i am sure this method will work. regards

      sAqIb "Our scientific power has outrun our spiritual power. We have guided missiles and misguided men." Dr. Martin Luther King Jr.

      1 Reply Last reply
      0
      • M mertkan65

        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; } }

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #3

        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) or if (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.

        M 1 Reply Last reply
        0
        • S Scott Dorman

          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) or if (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.

          M Offline
          M Offline
          mertkan65
          wrote on last edited by
          #4

          Thank you for your reply, I dont need internet avaliability, but it is useful. I think I can use NetworkAvailabilityChangedEventHandler, as you wrote. Thanks again;)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups