PowerSNMP for .NET
GetResponses(RequestMessage) Method
Example 




A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.
Sends a request and returns the responses. Can be used to discover multiple agents on a network.
Syntax
Public Overloads Function GetResponses( _
   ByVal request As RequestMessage _
) As ResponseMessage()
Dim instance As SnmpSocket
Dim request As RequestMessage
Dim value() As ResponseMessage
 
value = instance.GetResponses(request)
public ResponseMessage[] GetResponses( 
   RequestMessage request
)
public: ResponseMessage*[]* GetResponses( 
   RequestMessage* request
) 
public:
array<ResponseMessage^>^ GetResponses( 
   RequestMessage^ request
) 

Parameters

request
A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.

Return Value

A ResponseMessage[] from SNMP agents (or managers for InformMessages) that respond. Since multiple responses are typically received, this method reads packets from the socket until a timeout occurs.
Remarks

Use this method to request a Variable value and "discover" Agents available on the network. Note that some Agents and most SNMPv3 Agents will not respond to broadcast requests.

If Socket.AddressFamily is AddressFamily.InterNetwork, this method sends a Request to multiple agents (or managers) using the broadcast address of 255.255.255.255. For AddressFamily.InterNetworkV6, the request is sent to the "ff02::1" multicast group, which is commonly used to provide broadcast capabilities for IPv6.

If the request is an InformMessage then Manager.DefaultPort is used as the destination port (162); otherwise Agent.DefaultPort is used (161).

If using SNMPv3 one or more ReportMessages may be returned to indicate a communications failure.

The returned Response array may contain as few as 0 responses up to as many agents as are available on the network. Use Origin to determine the source of each ResponseMessage.

Example
The following example demonstrates how to discover agents on your network with a broadcast GetRequest.
private void buttonDiscoverAgents_Click(object sender, EventArgs e)
{
    //Create and send request on a worker thread
    manager1.Start(sendBroadcastGet, null);
}

/// <summary>
/// This method will send a GetMessage to the broadcast address, 
/// and any agents that respond will be added to a listview control.
/// </summary>
private void sendBroadcastGet(SnmpSocket managerSocket, object state)
{
    //Create Get message requesting a common OID
    GetMessage msg = new GetMessage();
    msg.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysUpTime));
    //Configure SnmpSocket.ReceiveTimeout to specify how long GetResponses() waits for responses here if needed.
    ResponseMessage[] responses = managerSocket.GetResponses(msg);

    //Marshal responses back to UI thread for display
    manager1.Marshal(responses, "discovery", null);
}

private void manager1_Message(object sender, Dart.Snmp.MessageEventArgs e)
{
    //Fires on the UI thread
    //Display each agent's IP in a listview
    foreach (ResponseMessage message in e.Messages)
        listView1.Items.Add(message.Origin.Address.ToString());
}
Private Sub buttonDiscoverAgents_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Create and send request on a worker thread
    manager1.Start(AddressOf sendBroadcastGet, Nothing)
End Sub

''' <summary>
''' This method will send a GetMessage to the broadcast address, 
''' and any agents that respond will be added to a listview control.
''' </summary>
Private Sub sendBroadcastGet(ByVal managerSocket As SnmpSocket, ByVal state As Object)
    'Create Get message requesting a common OID
    Dim msg As New GetMessage()
    msg.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysUpTime))
    'Configure SnmpSocket.ReceiveTimeout to specify how long GetResponses() waits for responses here if needed.
    Dim responses() As ResponseMessage = managerSocket.GetResponses(msg)

    'Marshal responses back to UI thread for display
    manager1.Marshal(responses, "discovery", Nothing)
End Sub

Private Sub manager1_Message(ByVal sender As Object, ByVal e As Dart.Snmp.MessageEventArgs)
    'Fires on the UI thread
    'Display each agent's IP in a listview
    For Each message As ResponseMessage In e.Messages
        listView1.Items.Add(message.Origin.Address.ToString())
    Next message
End Sub
See Also

Reference

SnmpSocket Class
SnmpSocket Members
Overload List


Documentation Version 4.9
© 2016 Dart Communications. All Rights Reserved.
Send comments on this topic