PowerSNMP for .NET
Start(NotificationReceived,IPEndPoint,Boolean,Object) Method
Example 




A NotificationReceived delegate specifying the method to execute when a message is received.
The local IPEndPoint the socket will bind to. Use port 162 for most applications. An IPv6 IPEndPoint is necessary to receive IPv6 traps.
Indicates whether the Manager should accept requests that are broadcast to the network. Must be false when using a dual mode socket.
Object to pass to the callback method; can be null.
Creates a socket and starts reading asynchronously. The specified user method executes on an IO Completion thread as each Trap1Message, Trap2Message or InformMessage is received.
Syntax
Public Overloads Sub Start( _
   ByVal callback As NotificationReceived, _
   ByVal localEP As IPEndPoint, _
   ByVal acceptBroadcast As Boolean, _
   ByVal state As Object _
) 
Dim instance As Manager
Dim callback As NotificationReceived
Dim localEP As IPEndPoint
Dim acceptBroadcast As Boolean
Dim state As Object
 
instance.Start(callback, localEP, acceptBroadcast, state)

Parameters

callback
A NotificationReceived delegate specifying the method to execute when a message is received.
localEP
The local IPEndPoint the socket will bind to. Use port 162 for most applications. An IPv6 IPEndPoint is necessary to receive IPv6 traps.
acceptBroadcast
Indicates whether the Manager should accept requests that are broadcast to the network. Must be false when using a dual mode socket.
state
Object to pass to the callback method; can be null.
Remarks

This method presents all asynchronous messages that may be received by a Manager. The localEP.AddressFamily must match the AddressFamily of the intended source of each message, unless using a dual mode socket.

If dual mode (simultaneous IPv4 and IPv6) socket operation is desired localEP address must be IPAddress.IPv6Any or an IPv6-mapped IPv4 address and the dualMode parameter must be true. An IPv4-mapped IPv6 address is the IPv4 address of the machine preceded by "::ffff:". Ex: ::ffff:192.168.1.1.

If an IPv4-mapped IPv6 socket is used then acceptBroadcast must be false. If accepting broadcast packets is required in dual mode, IPAddress.IPv6Any must be used.

This method always creates a new SocketBase.Socket. Use multiple Manager instances to accept traps on multiple sockets, or to accept both IPv4 and IPv6 traps.

If an InformMessage is received (from another manager or an agent), use SnmpBase.Send to send the expected ResponseMessage. No response is expected when a trap is received.

Unhandled exceptions occurring on the worker thread will be caught and reported by the Error event.

Scripting applications should use Receive instead.

Example
The following example demonstrates receiving notifications (Trap1, Trap2 and Inform messages), and responding to Inform messages.
private void button1_Click(object sender, EventArgs e)
{
    //Start listening for notifications
    manager1.Start(manager1_NotificationReceived, null);
}

private void manager1_NotificationReceived(Manager manager, MessageBase message, object state)
{
    //Marshal message to the UI thread using the Message event
    if (message is Trap1Message)
        manager.Marshal(new MessageBase[] { message }, "trap1", null);
    else if (message is Trap2Message)
        manager.Marshal(new MessageBase[] { message }, "trap2", null);
    else if (message is InformMessage)
    {
        manager.Marshal(new MessageBase[] { message }, "inform", null);

        //Send response to inform message origin
        manager.Send(new ResponseMessage(message as InformMessage, null), message.Origin);
    }
}

private void manager1_Message(object sender, MessageEventArgs e)
{
    //Update interface according to message type received
    switch (e.Message)
    {
        case "trap1":
            Trap1Message trap = e.Messages[0] as Trap1Message;
            label1.Text = "Trap1 received with Enterprise(" + trap.Enterprise + "), Generic Type (" +
            trap.GenericTrap.ToString() + "), Specific Type(" + trap.SpecificTrap.ToString() + ")";
            break;
        case "trap2":
            Trap2Message notification = e.Messages[0] as Trap2Message;
            label2.Text = "Trap2 received with OID (" + notification.Oid + ")";
            break;
        case "inform":
            InformMessage inform = e.Messages[0] as InformMessage;
            label3.Text = "Inform received with " + inform.Variables.Count.ToString() + " variable(s).";
            break;
    }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Start listening for notifications
    manager1.Start(AddressOf manager1_NotificationReceived, Nothing)
End Sub

Private Sub manager1_NotificationReceived(ByVal manager As Manager, ByVal message As MessageBase, ByVal state As Object)
    'Marshal message to the UI thread using the Message event
    If TypeOf message Is Trap1Message Then
        manager.Marshal(New MessageBase() { message }, "trap1", Nothing)
    ElseIf TypeOf message Is Trap2Message Then
        manager.Marshal(New MessageBase() { message }, "trap2", Nothing)
    ElseIf TypeOf message Is InformMessage Then
        manager.Marshal(New MessageBase() { message }, "inform", Nothing)

        'Send response to inform message origin
        manager.Send(New ResponseMessage(TryCast(message, InformMessage), Nothing), message.Origin)
    End If
End Sub

Private Sub manager1_Message(ByVal sender As Object, ByVal e As MessageEventArgs)
    'Update interface according to message type received
    Select Case e.Message
        Case "trap1"
            Dim trap As Trap1Message = TryCast(e.Messages(0), Trap1Message)
            label1.Text = "Trap1 received with Enterprise(" & trap.Enterprise & "), Generic Type (" & trap.GenericTrap.ToString() & "), Specific Type(" & trap.SpecificTrap.ToString() & ")"
        Case "trap2"
            Dim notification As Trap2Message = TryCast(e.Messages(0), Trap2Message)
            label2.Text = "Trap2 received with OID (" & notification.Oid & ")"
        Case "inform"
            Dim inform As InformMessage = TryCast(e.Messages(0), InformMessage)
            label3.Text = "Inform received with " & inform.Variables.Count.ToString() & " variable(s)."
    End Select
End Sub
See Also

Reference

Manager Class
Manager Members
Overload List

6.1.1.2
PowerSNMP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic