Advertisement
Guest User

Untitled

a guest
Apr 12th, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.NetworkInformation;
  6. using System.Text;
  7.  
  8. namespace IpDiscover
  9. {
  10.     class Program
  11.     {
  12.         static IPAddress[] GetIps1()
  13.         {
  14.             return Dns.GetHostAddresses(Dns.GetHostName());
  15.         }
  16.  
  17.         static IPAddress[] GetIps2()
  18.         {
  19.             List<IPAddress> list = new List<IPAddress>();
  20.  
  21.             NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
  22.             foreach (NetworkInterface adapter in interfaces)
  23.             {
  24.                 if (adapter.OperationalStatus == OperationalStatus.Up)
  25.                 {
  26.                     foreach (UnicastIPAddressInformation ipu in adapter.GetIPProperties().UnicastAddresses)
  27.                     {
  28.                         IPAddress ip = ipu.Address;
  29.                         list.Add(ip);
  30.                     }
  31.                 }
  32.             }
  33.  
  34.             return list.ToArray();
  35.         }
  36.        
  37.         static void Main(string[] args)
  38.         {
  39.             string out1 = "";
  40.             string out2 = "";
  41.  
  42.             Console.WriteLine("Please wait...");
  43.  
  44.             IPAddress[] ips = GetIps2();
  45.             foreach (IPAddress ip in ips)
  46.             {
  47.                 try
  48.                 {
  49.                     if (ip.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
  50.                         throw new Exception("Not an ipv4");
  51.  
  52.                     var request = (HttpWebRequest)HttpWebRequest.Create("http://www.clodo.it/projects/whatismyip/");
  53.                     request.KeepAlive = false;
  54.                     request.Timeout = 5000;
  55.                     request.ReadWriteTimeout = 5000;
  56.                     request.ServicePoint.BindIPEndPointDelegate = delegate
  57.                     {
  58.                         return new IPEndPoint(ip, 0);
  59.                     };
  60.                     var response = (HttpWebResponse)request.GetResponse();
  61.                     Stream receiveStream = response.GetResponseStream();
  62.                     StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
  63.  
  64.                     string result = readStream.ReadToEnd().Trim();
  65.                     out1 += result + "\r\n";
  66.                     out2 += ip.ToString() + ", " + result + "\r\n";
  67.                     response.Close();
  68.                 }
  69.                 catch (Exception e)
  70.                 {
  71.                     out2 += ip.ToString() + ", " + e.Message + "\r\n";
  72.                 }
  73.             }
  74.  
  75.             Console.WriteLine("");
  76.             //Console.WriteLine("Result:");
  77.             Console.WriteLine(out1);
  78.             //Console.WriteLine("Debug:");
  79.             //Console.WriteLine(out2);
  80.  
  81.             Console.WriteLine("Press any key to close.");
  82.             Console.ReadKey();
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement