我在 c++builder Xe(清单代码 1)中创建了以下例程,使用 Indi 组件 TIdSNMP 来查询代理 snmp。
对SendQuery的调用失败,连接正常,但代理始终不返回任何内容。
我还尝试使用QuickSend方法(清单代码 2)查询代理,在这种情况下,第二个例程工作正确,所以我想我在使用第一个例程时犯了一些错误。
有人能告诉我在第一个例程中我哪里错了吗?
最好的问候,恩佐
清单 1
void __fastcall TForm1::LabelSimpleSnmpCallClick(TObject * Sender)
{
String s1, s2, hostTarget = Edit_SnmpServer->Text, sysDescr = "1.3.6.1.2.1.1.1.0", sysUpTime = "1.3.6.1.2.1.1.3.0";
TIdSNMP * clientSnmp = 0;
TSNMPInfo * infoSnmp = 0;
std::auto_ptr< TStringList >sl1(new TStringList());
std::auto_ptr< TStringList >sl2(new TStringList());
Mylog(Format("Test simple SNMP call on server [%s]", ARRAYOFCONST((hostTarget))));
try
{
__try
{
clientSnmp = new TIdSNMP(NULL);
clientSnmp->Host = hostTarget;
clientSnmp->Community = "pubblic";
clientSnmp->ReceiveTimeout = 6000;
clientSnmp->Connect();
if (clientSnmp->Connected())
{
clientSnmp->Query->Clear();
clientSnmp->Query->MIBAdd(sysDescr, "");
clientSnmp->Query->MIBAdd(sysUpTime, "");
clientSnmp->Query->PDUType = PDUGetRequest;
clientSnmp->SendQuery();
infoSnmp = clientSnmp->Reply;
if (infoSnmp->ValueCount > 0)
{
sl1->Clear();
sl2->Clear();
sl1->AddStrings(infoSnmp->MIBOID);
sl2->AddStrings(infoSnmp->MIBValue);
for (int idx = 0; idx < sl1->Count; idx++)
{
s1 = sl1->Strings[idx];
s2 = sl2->Strings[idx];
Mylog(Format("Query on [%s] : [%s] => [%s]", ARRAYOFCONST((hostTarget, s1, s2))));
}
}
else
{
Mylog("*** No anwser *** ");
}
}
else
{
Mylog("*** No connected *** ");
}
}
__finally
{
if (clientSnmp)
{
delete clientSnmp;
clientSnmp = 0;
}
}
}
catch (Exception & ex)
{
Mylog(Format("ERROR [%s] ", ARRAYOFCONST((ex.Message))));
}
}
清单 2
void __fastcall TForm1::LabelQuickSendClick(TObject * Sender)
{
TIdSNMP * clientSnmp = 0;
String hostTarget = Edit_SnmpServer->Text, sysDescr = "1.3.6.1.2.1.1.1.0", sysUpTime = "1.3.6.1.2.1.1.3.0", val;
__try
{
clientSnmp = new TIdSNMP(NULL);
clientSnmp->ReceiveTimeout = 6000;
clientSnmp->QuickSend(sysDescr, "public", hostTarget, val);
Mylog(Format("Query on [%s] : [%s] => [%s]", ARRAYOFCONST((hostTarget, sysDescr, val))));
clientSnmp->QuickSend(sysUpTime, "public", hostTarget, val);
Mylog(Format("Query on [%s] : [%s] => [%s]", ARRAYOFCONST((hostTarget, sysUpTime, val))));
}
__finally
{
delete clientSnmp;
}
}