SnmpSharpNet.SnmpV2Packet.encode C# (CSharp) Method

encode() public method

Encode SNMP packet for sending.
public encode ( ) : byte[]
return byte[]
        public override byte[] encode()
        {
            MutableByte buf = new MutableByte();

            if (this.Pdu.Type != PduType.Get && this.Pdu.Type != PduType.GetNext &&
                this.Pdu.Type != PduType.Set && this.Pdu.Type != PduType.V2Trap &&
                this.Pdu.Type != PduType.Response && this.Pdu.Type != PduType.GetBulk &&
                this.Pdu.Type != PduType.Inform)
                throw new SnmpInvalidPduTypeException("Invalid SNMP PDU type while attempting to encode PDU: " + string.Format("0x{0:x2}", this.Pdu.Type));

            // snmp version
            _protocolVersion.encode(buf);

            // community string
            _snmpCommunity.encode(buf);

            // pdu
            _pdu.encode(buf);

            // wrap the packet into a sequence
            MutableByte tmpBuf = new MutableByte();
            AsnType.BuildHeader(tmpBuf, SnmpConstants.SMI_SEQUENCE, buf.Length);

            buf.Prepend(tmpBuf);
            return buf;
        }

Usage Example

 /// <summary>
 /// Send SNMP version 2 Trap notification
 /// </summary>
 /// <param name="packet">SNMP v2 Trap packet class</param>
 /// <param name="peer">Manager (receiver) IP address</param>
 /// <param name="port">Manager (receiver) UDP port number</param>
 public void SendV2Trap(SnmpV2Packet packet, IpAddress peer, int port)
 {
     if (packet.Pdu.Type != PduType.V2Trap)
     {
         throw new SnmpInvalidPduTypeException("Invalid Pdu type.");
     }
     byte[] outBuffer = packet.encode();
     _sock.SendTo(outBuffer, new IPEndPoint((IPAddress)peer, port));
 }
All Usage Examples Of SnmpSharpNet.SnmpV2Packet::encode