PowerSNMP for .NET
GetResponse(RequestMessage,IPEndPoint,Int32) Method
Example 




A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.
The IPEndPoint of the target agent or manager.
Requests will be resent this many times if a response is not received within the Socket.ReceiveTimeout period.
Sends a request to the specified IPEndPoint and returns the response.
Syntax
Public Overloads Function GetResponse( _
   ByVal request As RequestMessage, _
   ByVal target As IPEndPoint, _
   ByVal retries As Integer _
) As ResponseMessage
Dim instance As SnmpSocket
Dim request As RequestMessage
Dim target As IPEndPoint
Dim retries As Integer
Dim value As ResponseMessage
 
value = instance.GetResponse(request, target, retries)

Parameters

request
A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.
target
The IPEndPoint of the target agent or manager.
retries
Requests will be resent this many times if a response is not received within the Socket.ReceiveTimeout period.

Return Value

The ResponseMessage from the remote host.
Exceptions
ExceptionDescription
System.Net.Sockets.SocketExceptionA problem was experienced using the Socket or the Socket.ReceiveTimeout period has expired.
DecodingExceptionA problem was encountered decoding the Response.
Remarks

This method always returns a valid ResponseMessage (or throws an exception).

Agents typically listen on port 161 but can be configured to listen on any port. If sending an InformMessage to another Manager, port 162 is usually used.

SNMPv3 discovery of the recipient's engine id and time is automatic and cached in SessionSecurity.EngineCache. Encrypted passwords are also cached to improve performance. A ReportMessage may be returned to indicate a failure.

Example
The following example demonstrates sending an SNMPv1 Get request to an agent and receiving the response.
private void button1_Click(object sender, EventArgs e)
{
    //Create and send request on a worker thread
    manager1.Start(manager1_SendGetRequest, manager1.Mib.CreateVariable(NodeName.sysContact));
}

private void button2_Click(object sender, EventArgs e)
{
    //If you don't have the MIB, retrieve the value by IID:
    manager1.Start(manager1_SendGetRequest, new Variable("1.3.6.1.2.1.1.4.0"));
}

private void manager1_SendGetRequest(SnmpSocket managerSocket, object state)
{
    //Create Get Request
    GetMessage request = new GetMessage();
    request.Variables.Add(state as Variable);

    //Send request and get response
    ResponseMessage response = managerSocket.GetResponse(request, myAgentAddress);

    //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() + "\r\n";
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Create and send request on a worker thread
    manager1.Start(AddressOf manager1_SendGetRequest, manager1.Mib.CreateVariable(NodeName.sysContact))
End Sub

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    'If you don't have the MIB, retrieve the value by IID:
    manager1.Start(AddressOf manager1_SendGetRequest, New Variable("1.3.6.1.2.1.1.4.0"))
End Sub

Private Sub manager1_SendGetRequest(ByVal managerSocket As SnmpSocket, ByVal state As Object)
    'Create Get Request
    Dim request As New GetMessage()
    request.Variables.Add(TryCast(state, Variable))

    'Send request and get response
    Dim response As ResponseMessage = managerSocket.GetResponse(request, myAgentAddress)

    '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() & vbCrLf
End Sub
See Also

Reference

SnmpSocket Class
SnmpSocket Members
Overload List


PowerSNMP for .NET Documentation Version 8.0
© 2024 Dart Communications. All Rights Reserved.
Send comments on this topic