ASN.1 IPAddress type implementation

Namespace: SnmpSharpNet
Assembly: SnmpSharpNet (in SnmpSharpNet.dll) Version: 0.9.1.0 (0.9.1)

Syntax

C#
[SerializableAttribute]
public class IpAddress : OctetString, IComparable, 
	ICloneable
Visual Basic
<SerializableAttribute> _
Public Class IpAddress _
	Inherits OctetString _
	Implements IComparable, ICloneable
Visual C++
[SerializableAttribute]
public ref class IpAddress : public OctetString, 
	IComparable, ICloneable

Remarks

You can assign the IP address value to the class using: A string value representing the IP address in dotted decimal notation:
CopyC#
IpAddress ipaddr = new IpAddress("10.1.1.1");
A string value representing the hosts domain name:
CopyC#
IpAddress ipaddr = new IpAddress("this.ismyhost.com");
Another IpAddress class:
CopyC#
IpAddress first = new IpAddress("10.1.1.2");
IpAddress second = new IpAddress(first);
Or from the System.Net.IPAddress:
CopyC#
IPAddress addr = IPAddress.Any;
IpAddress ipaddr = new IpAddress(addr);
You can check if the IpAddress class contains a valid value by calling:
CopyC#
IpAddress ipaddr = new IpAddress("10.1.1.3");
if( ! ipaddr.Valid ) {
  Console.WriteLine("Invalid IP Address value.");
  return;
}
There are other operations you can perform with the IpAddress class. For example, let say you retrieved an IP address and subnet mask from an SNMP agent and wish to scan the subnet for other hosts that can be managed. You could do this: IpAddress host = SomehowRetrieveIPAddress(); IpAddress mask = SomehowRetrieveSubnetMask(); IpAddress subnetAddr = host.GetSubnetAddress(mask); IpAddress broadcastAddr = host.GetBroadcastAddress(mask); IpAddress host = (IpAddress)subnetAddr.Clone(); while( host.CompareTo(broadcastAddr) != 0 ) { host = host.Increment(1); // increment IP address by one if( ! host.Equals(broadcastAddr) ) ScanHostInWhateverWayYouLike(host); } Note: internally, IpAddress class holds the value in a byte array, network ordered format.

Inheritance Hierarchy

System..::..Object
  SnmpSharpNet..::..AsnType
    SnmpSharpNet..::..OctetString
      SnmpSharpNet..::..IpAddress

See Also