What is the best way to check for Internet connectivity using .NET?

后端 未结 27 2436
感动是毒
感动是毒 2020-11-22 07:41

What is the fastest and most efficient way to check for Internet connectivity in .NET?

27条回答
  •  暖寄归人
    2020-11-22 08:18

    Multi threaded version of ping:

      using System;
      using System.Collections.Generic;
      using System.Diagnostics;
      using System.Net.NetworkInformation;
      using System.Threading;
    
    
      namespace OnlineCheck
      {
          class Program
          {
    
              static bool isOnline = false;
    
              static void Main(string[] args)
              {
                  List ipList = new List {
                      "1.1.1.1", // Bad ip
                      "2.2.2.2",
                      "4.2.2.2",
                      "8.8.8.8",
                      "9.9.9.9",
                      "208.67.222.222",
                      "139.130.4.5"
                      };
    
                  int timeOut = 1000 * 5; // Seconds
    
    
                  List threadList = new List();
    
                  foreach (string ip in ipList)
                  {
    
                      Thread threadTest = new Thread(() => IsOnline(ip));
                      threadList.Add(threadTest);
                      threadTest.Start();
                  }
    
                  Stopwatch stopwatch = Stopwatch.StartNew();
    
                  while (!isOnline && stopwatch.ElapsedMilliseconds <= timeOut)
                  {
                       Thread.Sleep(10); // Cooldown the CPU
                  }
    
                  foreach (Thread thread in threadList)
                  { 
                      thread.Abort(); // We love threads, don't we?
                  }
    
    
                  Console.WriteLine("Am I online: " + isOnline.ToYesNo());
                  Console.ReadKey();
              }
    
              static bool Ping(string host, int timeout = 3000, int buffer = 32)
              {
                  bool result = false;
    
                  try
                  {
                      Ping ping = new Ping();                
                      byte[] byteBuffer = new byte[buffer];                
                      PingOptions options = new PingOptions();
                      PingReply reply = ping.Send(host, timeout, byteBuffer, options);
                      result = (reply.Status == IPStatus.Success);
                  }
                  catch (Exception ex)
                  {
    
                  }
    
                  return result;
              }
    
              static void IsOnline(string host)
              {
                  isOnline =  Ping(host) || isOnline;
              }
          }
    
          public static class BooleanExtensions
          {
              public static string ToYesNo(this bool value)
              {
                  return value ? "Yes" : "No";
              }
          }
      }
    

提交回复
热议问题