Esempio n. 1
0
 public static IPAddress QueryActiveIPAddress(int timeout = 300, string hostNameOrAddress = "www.baidu.com")
 {
     if (string.IsNullOrEmpty(hostNameOrAddress))
     {
         return(null);
     }
     using (AutoResetEvent events = new AutoResetEvent(false))
     {
         IPAddress address    = null;
         Stopwatch stopwatch  = new Stopwatch();
         bool      istimedout = false;
         try
         {
             Dns.BeginGetHostAddresses(hostNameOrAddress, (ar) =>
             {
                 if (istimedout)
                 {
                     return;
                 }
                 try
                 {
                     IPAddress[] addresses = Dns.EndGetHostAddresses(ar);
                     if (addresses != null && addresses.Length > 0)
                     {
                         foreach (IPAddress i in addresses)
                         {
                             if (i.AddressFamily == AddressFamily.InterNetwork)
                             {
                                 address = i;
                                 break;
                             }
                         }
                         events.Set();
                     }
                 }
                 catch (Exception)
                 {
                 }
             }, null);
         }
         catch (Exception)
         {
             return(null);
         }
         do
         {
             stopwatch.Start();
             if (!events.WaitOne(timeout))
             {
                 istimedout = true;
                 return(null);
             }
             else if (address == null)
             {
                 return(null);
             }
             stopwatch.Stop();
         } while (false);
         timeout -= Convert.ToInt32(stopwatch.ElapsedMilliseconds);
         if (timeout <= 0)
         {
             return(null);
         }
         using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
         {
             try
             {
                 socket.NoDelay = true;
                 socket.BeginConnect(new IPEndPoint(address, 443), (ar) =>
                 {
                     if (istimedout)
                     {
                         return;
                     }
                     try
                     {
                         socket.EndConnect(ar);
                         EndPoint localEP = socket.LocalEndPoint;
                         if (localEP != null)
                         {
                             IPEndPoint ipep = (IPEndPoint)localEP;
                             address         = ipep.Address;
                             events.Set();
                         }
                     }
                     catch (Exception)
                     {
                         address = null;
                     }
                 }, null);
             }
             catch (Exception)
             {
                 return(null);
             }
             do
             {
                 if (!events.WaitOne(timeout))
                 {
                     istimedout = true;
                     return(null);
                 }
             } while (false);
             MalockSocket.Close(socket);
         }
         return(address);
     }
 }