Visual Basic (Declaration) | |
---|---|
Public Function Receive( _ ByVal request As RequestMessage _ ) As ResponseMessage |
Visual Basic (Usage) | ![]() |
---|---|
Dim instance As ManagerSlave Dim request As RequestMessage Dim value As ResponseMessage value = instance.Receive(request) |
C# | |
---|---|
public ResponseMessage Receive( RequestMessage request ) |
Managed Extensions for C++ | |
---|---|
public: ResponseMessage* Receive( RequestMessage* request ) |
C++/CLI | |
---|---|
public: ResponseMessage^ Receive( RequestMessage^ request ) |
Parameters
- request
- An optional instance of GetMessage, GetNextMessage, GetBulkMessage, SetMessage or InformMessage. Can be null.
Return Value
The ResponseMessage from the remote host.Exception | Description |
---|---|
System.Net.Sockets.SocketException | A problem was experienced using the Socket or the Socket.ReceiveTimeout period has expired. |
Dart.Snmp.DecodingException | A problem was encountered decoding the Response. |
Dart.Snmp.SecurityException | A problem was encountered with the Security attributes of the Request or the Response. |
The following example demonstrates how to send requests and receive responses using the same port.
C# | ![]() |
---|---|
string myAgentAddress = "192.168.0.11"; ManagerSlave managerSlave = null; private void button1_Click(object sender, EventArgs e) { //Start listening for responses on a specified port manager1.Start(new SlaveThreadStart(receiveResponses), new IPEndPoint(IPAddress.Any, 777)); } private void button2_Click(object sender, EventArgs e) { //Send a request to myAgentAddress manager1.Start(new SlaveThreadStart(sendRequest), new IPEndPoint(IPAddress.Parse(myAgentAddress), 161)); } private void receiveResponses(ManagerSlave slave, object state) { //Continue receiving responses until the managerSlave is closed managerSlave = slave; managerSlave.LocalEndPoint = (IPEndPoint)state; managerSlave.Socket.ReceiveTimeout = 0; ResponseMessage response = null; do { try {response = managerSlave.Receive(null);} catch (Exception ex) { manager1.Marshal(new Exception("The socket is no longer receiving", ex)); break; } //Marshal response to UI thread manager1.Marshal(new MessageBase[] { response }, null); } while(managerSlave.Socket != null); } private void sendRequest(ManagerSlave slave, object state) { //Create and send a Get Request using existing slave try { GetMessage request = new GetMessage(); request.Version = SnmpVersion.One; request.Community = "public"; request.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysUpTime)); //Use managerSlave to send the request managerSlave.Send(request, (IPEndPoint)state); } catch (Exception ex) { manager1.Marshal(ex); } } private void manager1_Message(object sender, MessageEventArgs e) { //Display response ResponseMessage response = (ResponseMessage)e.Messages[0]; displayResponse(DateTime.Now.ToString() + " : " + response.Variables[0].Value.ToString()); } private void displayResponse(string s) { ... } |
Visual Basic | ![]() |
---|---|
Private myAgentAddress As String = "192.168.0.11" Private managerSlave As ManagerSlave = Nothing Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) 'Start listening for responses on a specified port manager1.Start(New SlaveThreadStart(AddressOf receiveResponses), New IPEndPoint(IPAddress.Any, 777)) End Sub Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) 'Send a request to myAgentAddress manager1.Start(New SlaveThreadStart(AddressOf sendRequest), New IPEndPoint(IPAddress.Parse(myAgentAddress), 161)) End Sub Private Sub receiveResponses(ByVal slave As ManagerSlave, ByVal state As Object) 'Continue receiving responses until the managerSlave is closed managerSlave = slave managerSlave.LocalEndPoint = CType(state, IPEndPoint) managerSlave.Socket.ReceiveTimeout = 0 Dim response As ResponseMessage = Nothing Do Try response = managerSlave.Receive(Nothing) Catch ex As Exception manager1.Marshal(New Exception("The socket is no longer receiving", ex)) Exit Do End Try 'Marshal response to UI thread manager1.Marshal(New MessageBase() { response }, Nothing) Loop While Not managerSlave.Socket Is Nothing End Sub Private Sub sendRequest(ByVal slave As ManagerSlave, ByVal state As Object) 'Create and send a Get Request using existing slave Try Dim request As GetMessage = New GetMessage() request.Version = SnmpVersion.One request.Community = "public" request.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysUpTime)) 'Use managerSlave to send the request managerSlave.Send(request, CType(state, IPEndPoint)) Catch ex As Exception manager1.Marshal(ex) End Try End Sub Private Sub manager1_Message(ByVal sender As Object, ByVal e As MessageEventArgs) 'Display response Dim response As ResponseMessage = CType(e.Messages(0), ResponseMessage) displayResponse(DateTime.Now.ToString() & " : " & response.Variables(0).Value.ToString()) End Sub Private Sub displayResponse(ByVal s As String) ... End Sub |
This method blocks and does not return until either a response from the agent is received or a socket timeout occurs. It should be used on a worker thread with Send (being called on another thread) when a single socket must be used for handling responses from multiple outstanding requests in an asynchronous manner.
This method may be used on a worker threat to received Agent responses from multiple requests using Send from another thread.
Target Platforms: Microsoft .NET Framework 2.0