GetLocalIPAddresses() public static method

Returns an array of all the IPAddresses of the local machine. Manually add loopback sense Windows does not have a loopback interface.
public static GetLocalIPAddresses ( ) : System.Net.IPAddress[]
return System.Net.IPAddress[]
コード例 #1
0
 public void Test() {
   IPAddress[] base_addresses = Dns.GetHostAddresses(string.Empty);
   ArrayList local_ips = new ArrayList(base_addresses);
   local_ips.Add(IPAddress.Loopback);
   ArrayList ips = new ArrayList(IPHandler.GetLocalIPAddresses());
   foreach(IPAddress addr in local_ips) {
     Assert.IsTrue(ips.Contains(addr), addr + " is not in ips");
   }
   Assert.AreEqual(ips.Count, local_ips.Count, "Count");
 }
コード例 #2
0
ファイル: IPHandler.cs プロジェクト: pcbing/brunet
 /**
  * <summary>Sends the data over the multicast socket.</summary>
  * <param name="data">The data to send.</summary>
  */
 public override void Send(ICopyable data)
 {
     IPAddress[] ips = LocalIPAddresses;
     if (ips == null)
     {
         ips = IPHandler.GetLocalIPAddresses();
     }
     // Silly users can trigger a handful of exceptions here...
     try {
         data = new CopyList(IPHandler.MagicCookie, data);
         byte[] buffer = new byte[data.Length];
         int    length = data.CopyTo(buffer, 0);
         // I REALLY HATE THIS but we can't be setting this option in more than one thread!
         lock (_s) {
             foreach (IPAddress ip in ips)
             {
                 /*
                  * This can throw an exception on an invalid address, we need to skip it and move on!
                  * Never showed to be an issue in Linux, but Windows does some weird things.
                  */
                 try {
                     _s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface,
                                        IPHandler.IPAddressToInt(ip));
                 }
                 catch {
                     continue;
                 }
                 _s.SendTo(buffer, 0, length, 0, EndPoint);
             }
         }
     }
     catch (System.Net.Sockets.SocketException sx) {
         throw new SendException(true, "SocketException", sx);
     }
     // Can't pass the fact that the IPHandler is not running :-/
     catch (ObjectDisposedException odx) {
         throw new SendException(false, "Socket appears to be disposed", odx);
     }
     catch (Exception e) {
         ProtocolLog.WriteIf(ProtocolLog.Exceptions, "ERROR: " + e);
         throw new SendException(true, "Socket appears to be disposed", e);
     }
 }