PowerSNMP for .NET CF
Send Method
See Also  Example Send comments on this topic.
Dart.Snmp Namespace > ManagerSlave Class : Send Method




request
An instance of GetMessage, GetNextMessage, GetBulkMessage, SetMessage or InformMessage.
remoteEP
The IPEndPoint specifying the target agent or manager.
Sends a RequestMessage to the specified agent on the specifed port.

Syntax

Visual Basic (Declaration) 
Public Sub Send( _
   ByVal request As RequestMessage, _
   ByVal remoteEP As IPEndPoint _
) 
Visual Basic (Usage)Copy Code
Dim instance As ManagerSlave
Dim request As RequestMessage
Dim remoteEP As IPEndPoint
 
instance.Send(request, remoteEP)
C# 
public void Send( 
   RequestMessage request,
   IPEndPoint remoteEP
)
Managed Extensions for C++ 
public: void Send( 
   RequestMessage* request,
   IPEndPoint* remoteEP
) 
C++/CLI 
public:
void Send( 
   RequestMessage^ request,
   IPEndPoint^ remoteEP
) 

Parameters

request
An instance of GetMessage, GetNextMessage, GetBulkMessage, SetMessage or InformMessage.
remoteEP
The IPEndPoint specifying the target agent or manager.

Exceptions

ExceptionDescription
System.Net.Sockets.SocketExceptionA problem was experienced using the Socket.
Dart.Snmp.SecurityExceptionA problem was encountered with the Security attributes of the Request.

Example

The following example demonstrates how to send requests and receive responses using the same port.
C#Copy Code
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 BasicCopy Code
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

Remarks

This method encodes and sends a message, performing any necessary discovery if using SNMPv3. It is used with Receive when a single socket must be used for handling responses from multiple outstanding requests in a synchronous blocking manner.

This method checks the LocalEndPoint property for an AddressFamily that matches the remoteEP parameter. If they do not match, then the LocalEndPoint property is reset to the remoteEP.AddressFamily and the Socket is recreated. Consequently, multiple IPv4 and IPv6 hosts can be contacted using a single ManagerSlave object.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

Documentation Version 4.2
© 2010 Dart Communications. All Rights Reserved.