📄 trap.pas
字号:
unit Trap;
interface
uses
Classes, Snmpsend, SysUtils,Variants, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, NMUDP,
IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer;
type
snmpRecvTrap = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
uses
Main;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure snmpRecvTrap.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ snmpRecvTrap }
procedure snmpRecvTrap.Execute;
var
Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer;
MIBName, MIBValue: TStringList;
i,iResult,LoopCount : integer;
SNMPSend: TSNMPSend;
function CheckPort : boolean;
var
UDP :TidUDPServer;
begin
UDP := TidUDPServer.Create(nil);
UDP.DefaultPort := 162;
try
UDP.Active := true;
result := true;
UDP.Active := false;
except
result := false;
end;
UDP.Free;
end;
begin
Count := 0;
Start := true;
FreeOnTerminate := true;
if not CheckPort then
begin
form1.Memo1.Lines.Add('Port 162 is already in use ! Thread Ternimated !') ;
exit;
end;
MIBName := TStringList.Create;
MIBValue := TStringList.Create;
SNMPSend := TSNMPSend.Create;
try
SNMPSend.TargetPort := cSnmpTrapProtocol;
while(true) do
begin
if Terminated then exit;
form1.memo1.lines.add('Receive Trap Loop: '+IntToStr(LoopCount));
if (RecvTrap(SNMPSend, Dest, Source, Enterprise, Community,
Generic, Specific, Seconds, MIBName, MIBValue)<>0) then
begin
Inc(Count,1);
with Form1 do
begin
Memo1.Lines.Add('Destination: '+Dest);
Memo1.Lines.Add('Source: '+Source);
Memo1.Lines.Add('Enterprise: '+Enterprise);
Memo1.Lines.Add('Community: '+Community);
Memo1.Lines.Add('Generic: '+IntToStr(Generic));
Memo1.Lines.Add('Specific: '+IntToStr(Specific));
Memo1.Lines.Add('Seconds: '+IntToStr(Seconds));
for i:= 0 to MIBName.Count-1 do
begin
Memo1.Lines.Add('MIBName['+IntToStr(i)+']'+MIBName[i]);
Memo1.Lines.Add('MIBValue['+IntToStr(i)+']'+MIBValue[i]);
end;
Memo1.Lines.Add('-------------------------------------');
end;
MIBName.Clear;
MIBValue.Clear;
end;
Inc(LoopCount,1);
end;
finally
SNMPSend.Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -