Dart.Snmp Namespace > SnmpSocket Class : GetResponseAsync Method |
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)
public void GetResponseAsync( RequestMessage request, IPEndPoint target, int timeout, int retries, ResponseReceived callback, object state )
public: void GetResponseAsync( RequestMessage* request, IPEndPoint* target, int timeout, int retries, ResponseReceived* callback, Object* state )
public: void GetResponseAsync( RequestMessage^ request, IPEndPoint^ target, int timeout, int retries, ResponseReceived^ callback, Object^ state )
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.
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