2017-11-10 16 views

答えて

0

をマスクしていない「NetworkInformation」クラスのみIPを提供し、現在

をマスクしないで、直接、サブネットマスクを取得するには、そのようなAPIはありません。代わりにPrefixLengthを取得できます。そしてPrefixLengthSubnetマスクに変換するには、次のようにします。

var hosts = NetworkInformation.GetHostNames(); 

foreach (var host in hosts) 
{ 
    if (host.Type == HostNameType.Ipv4 && host.IPInformation != null) 
    { 
     var submask = SubnetMask.CreateByNetBitLength((int)host.IPInformation.PrefixLength); 
    } 
} 

サブネットヘルパー

public static class SubnetMask 
{ 
    public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0"); 
    public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0"); 
    public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0"); 

    public static IPAddress CreateByHostBitLength(int hostpartLength) 
    { 
     int hostPartLength = hostpartLength; 
     int netPartLength = 32 - hostPartLength; 

     if (netPartLength < 2) 
      throw new ArgumentException("Number of hosts is to large for IPv4"); 

     Byte[] binaryMask = new byte[4]; 

     for (int i = 0; i < 4; i++) 
     { 
      if (i * 8 + 8 <= netPartLength) 
       binaryMask[i] = (byte)255; 
      else if (i * 8 > netPartLength) 
       binaryMask[i] = (byte)0; 
      else 
      { 
       int oneLength = netPartLength - i * 8; 
       string binaryDigit = 
        String.Empty.PadLeft(oneLength, '1').PadRight(8, '0'); 
       binaryMask[i] = Convert.ToByte(binaryDigit, 2); 
      } 
     } 
     return new IPAddress(binaryMask); 
    } 

    public static IPAddress CreateByNetBitLength(int netpartLength) 
    { 
     int hostPartLength = 32 - netpartLength; 
     return CreateByHostBitLength(hostPartLength); 
    } 

    public static IPAddress CreateByHostNumber(int numberOfHosts) 
    { 
     int maxNumber = numberOfHosts + 1; 

     string b = Convert.ToString(maxNumber, 2); 

     return CreateByHostBitLength(b.Length); 
    } 
} 
+0

だから、同じことがまた、C++/Cxの中で正しく動作する必要がありますか? –

+0

更新情報がありますか? –

+0

私はまだC++/cxでコードを書いています。もし私があなたに何かを教えてくれるなら、...ありがとう:) –

関連する問題