I'm using c++builderXE with Indy 10.5.7 and I'm trying to receive trap from another agent snmp.
I have no info describing how to do the program to receive trap.
Below you can find the snippet of code which I'm trying to use now.
The ReceiveTrap() method always return 0, which means non data received.
I tested the PC configuration with another program I made several years ago using spare API and the trap is received so I don't this it should be a configuration problem.
Have you some suggestions of hat I'm wrong in the routine below?
void __fastcall TForm1::LabelReceiveTrapClick(TObject * Sender)
{
static bool status = false;
int ists;
String Fun = "[SimpleReceiveTrap] ";
TSNMPInfo * infoSnmp = 0;
try
{
status = !status;
if (status)
{
std::auto_ptr< TIdSNMP >clientSnmp(new TIdSNMP(NULL));
clientSnmp->Community = "public";
clientSnmp->ReceiveTimeout = 1000;
clientSnmp->Binding->Port = 162;
while (status)
{
Application->ProcessMessages();
ists = clientSnmp->ReceiveTrap();
Mylog(L"%s ReceiveTrap status = [%d]", Fun.c_str(), ists);
if (ists > 0)
{
infoSnmp = clientSnmp->Trap;
}
}
}
}
catch (Exception & ex)
{
Mylog(L"%s ERROR", Fun.c_str(), ex.Message.c_str());
}
}
That is not the correct way to set the listening Port for receiving traps. Reading the
Binding
property allocates and binds a socket to a local IP/Port using theTIdSNMP::BoundIP
andTIdSNMP::BoundPort
properties. You can't change that socket's localPort
after it has already been bound, so your assignment of theBinding->Port
property is effectively a no-op.For that matter, you are trying to manipulate the wrong socket anyway. The
Binding
socket is used for sending queries to the remote SNMP system.TIdSNMP
uses a separate socket for receiving traps.TIdSNMP
has a separateTrapPort
property for specifying the listening Port of that socket. When theBinding
is accessed, the trap socket is allocated and bound toBinding->IP
andTIdSNMP::TrapPort
. TheTrapPort
property defaults to 162.Looking at Indy's changelog, there have been some trap-related changes to the listening socket since 10.5.7 was released, so you may need to upgrade to a newer Indy version to get bug fixes. Or you could download the latest version and then just add
IdSNMP.pas
to your project directly, at least.