PowerSNMP for .NET
GetResponseAsync Method
Example 




A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.
The IPEndPoint of the target agent or manager.
Timeout, in milliseconds, to wait for the response. 3000 is a typical value. 0 should not be used.
Requests will be resent this many times if a response is not received within the Socket.ReceiveTimeout period.
A ResponseReceived delegate referencing the method to invoke on an IO completion thread when the operation completes.
Object to pass to the callback method when the operation completes; can be null.
Sends a request to the specified target and returns the response by invoking the callback method on an IO completion thread.
Syntax
Public Sub GetResponseAsync( _
   ByVal request As RequestMessage, _
   ByVal target As IPEndPoint, _
   ByVal timeout As Integer, _
   ByVal retries As Integer, _
   ByVal callback As ResponseReceived, _
   ByVal state As Object _
) 
Dim instance As SnmpSocket
Dim request As RequestMessage
Dim target As IPEndPoint
Dim timeout As Integer
Dim retries As Integer
Dim callback As ResponseReceived
Dim state As Object
 
instance.GetResponseAsync(request, target, timeout, retries, callback, state)

Parameters

request
A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.
target
The IPEndPoint of the target agent or manager.
timeout
Timeout, in milliseconds, to wait for the response. 3000 is a typical value. 0 should not be used.
retries
Requests will be resent this many times if a response is not received within the Socket.ReceiveTimeout period.
callback
A ResponseReceived delegate referencing the method to invoke on an IO completion thread when the operation completes.
state
Object to pass to the callback method when the operation completes; can be null.
Remarks

If your application uses SnmpSocket.GetResponse() on parallel threads, consider using this method instead to launch those requests asynchronously. This technique avoids the overhead of multiple worker threads.

This method causes a byte array to be pinned while waiting for a response. If a response is not received, this byte array will be pinned until the timeout period expires. If an infinite timeout is specified this byte array will remain pinned. This method can be cancelled by closing the underlying Socket.

Example
The following example demonstrates sending an SNMPv1 Get request to an agent and receiving the response asynchronously.
private void button1_Click(object sender, EventArgs e)
{
    //Create Get Request
    GetMessage request = new GetMessage();
    request.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysContact));

    //Create socket for sending request from. Binds to IPAddress.Any, port 0.
    SnmpSocket managerSocket = new SnmpSocket(manager1);

    //Send request and receive response. managerSocket_ResponseReceived is invoked when the response is received.
    managerSocket.GetResponseAsync(request, myAgentAddress, 3000, 3, managerSocket_ResponseReceived, null);
}

/// <summary>
/// Invoked on an IO completion thread when the response is received.
/// </summary>
/// <param name="request">The corresponding RequestMessage.</param>
/// <param name="response">Response from the target agent or manager. Null if a response was not received.</param>
/// <param name="ex">Populated if the operation threw an exception.</param>
/// <param name="state">Object passed into the state parameter of GetResponseAsync. Not used in this snippet.</param>
private void managerSocket_ResponseReceived(RequestMessage request, ResponseMessage response, Exception ex, object state)
{
    //Marshal response to the UI thread using the Message event
    manager1.Marshal(new ResponseMessage[] { response }, "", null);
}

private void manager1_Message(object sender, MessageEventArgs e)
{
    //Display info about the first variable in the response, and its value
    Variable vari = e.Messages[0].Variables[0];
    label1.Text += vari.Definition.ToString() + vari.Value.ToString() + Environment.NewLine;
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Create Get Request
    Dim request As New GetMessage()
    request.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysContact))

    'Create socket for sending request from. Binds to IPAddress.Any, port 0.
    Dim managerSocket As New SnmpSocket(manager1)

    'Send request and receive response. managerSocket_ResponseReceived is invoked when the response is received.
    managerSocket.GetResponseAsync(request, myAgentAddress, 3000, 3, AddressOf managerSocket_ResponseReceived, Nothing)
End Sub

''' <summary>
''' Invoked on an IO completion thread when the response is received.
''' </summary>
''' <param name="request">The corresponding RequestMessage.</param>
''' <param name="response">Response from the target agent or manager. Null if a response was not received.</param>
''' <param name="ex">Populated if the operation threw an exception.</param>
''' <param name="state">Object passed into the state parameter of GetResponseAsync. Not used in this snippet.</param>
Private Sub managerSocket_ResponseReceived(ByVal request As RequestMessage, ByVal response As ResponseMessage, ByVal ex As Exception, ByVal state As Object)
    'Marshal response to the UI thread using the Message event
    manager1.Marshal(New ResponseMessage() { response }, "", Nothing)
End Sub

Private Sub manager1_Message(ByVal sender As Object, ByVal e As MessageEventArgs)
    'Display info about the first variable in the response, and its value
    Dim vari As Variable = e.Messages(0).Variables(0)
    label1.Text &= vari.Definition.ToString() & vari.Value.ToString() & Environment.NewLine
End Sub
See Also

Reference

SnmpSocket Class
SnmpSocket Members

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