如何寫一個ping程序????(40分)

  • 主题发起人 主题发起人 mynone
  • 开始时间 开始时间
M

mynone

Unregistered / Unconfirmed
GUEST, unregistred user!
用idicmpclientl來寫一個ping程序,要是多開幾個窗口來ping多個地址,系統就提示內存不足了???
我用idicmpclientl來每隔50ms就ping一次
1,(是否可以不用定時器,讓它做完就就delay 50ms不知會不會有幫助.)
2,是否有其它更好的ping方法
procedure TForm1.Timer1Timer(Sender: TObject);
var
idIcmp:TIdIcmpClient;
begin
try
idIcmp := TIdIcmpClient.Create(nil);
idIcmp.ReceiveTimeout := strtoint(edit1.text);
idIcmp.Host :=edit2.Text;
idicmp.BufferSize:=8192;
idIcmp.Ping;
if idIcmp.ReplyStatus.ReplyStatusType=rsEcho then
begin
if ProgressBar1.Position>=100 then
ProgressBar1.Position:=0;
ProgressBar1.Position:=ProgressBar1.Position+10;
exit;
end;
finally
idicmp.Destroy;
end;
windows.Beep(1047,500);
end;
 
全文检索一下吧,建议用多线程来ping好了.还有就是你可以到www.2ccc.com上去下载源码,上面有!
 
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, winsock, StdCtrls, clipbrd, ComCtrls, ToolWin, ExtCtrls, Buttons,
Gauges;

type
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation = packed record
TTL: Byte;
TOS: Byte;
Flags: Byte;
OptionsSize: Byte;
OptionsData: PChar;
end;
type PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = packed record
Address: DWORD;
Status: DWORD;
RTT: DWORD;
DataSize:Word;
Reserved: Word;
Data: Pointer;
Options: TIPOptionInformation;
end;
TIcmpCreateFile = function: THandle; stdcall;
TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall;
TIcmpSendEcho = function(IcmpHandle:THandle;
DestinationAddress:DWORD;
RequestData: Pointer;
RequestSize: Word;
RequestOptions: PIPOptionInformation;
ReplyBuffer: Pointer;
ReplySize: DWord;
Timeout: DWord): DWord; stdcall;

procedure TFrm_ListIP.FormCreate(Sender: TObject);
begin
hICMPdll := LoadLibrary('icmp.dll');
@ICMPCreateFile:= GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
end;

procedure TFrm_ListIP.FormDestroy(Sender: TObject);
begin
IcmpCloseHandle(hicmp);
FreeLibrary(hicmpdll);
end;

function PingAssignedIP(sIP : String):Boolean;
var
IPOpt:TIPOptionInformation;
FIPAddress:DWORD;
pReqData,pRevData:PChar;
pIPE:PIcmpEchoReply;
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
begin
Result := False;
if(sIP='')then
begin
MessageBox(Handle,'The assigned IP address is empty!','List free IP',MB_OK+MB_ICONWARNING);
exit;
end;

FIPAddress:=inet_addr(PChar(sIP));
if(FIPAddress=INADDR_NONE)then
begin
MessageBox(Handle,'The assigned IP address is invalid!','List free IP',MB_OK+MB_ICONWARNING);
exit;
end;
FSize:= 40;
BufferSize:=SizeOf(TICMPEchoReply)+FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Looking for host.';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL:= 255;
FTimeOut := 5;
IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE,BufferSize, FTimeOut);
try
try
if pReqData^ = pIPE^.Options.OptionsData^ then
Result := False
else Result := True;
except
Result := True;
end;
finally
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;
 
多人接受答案了。
 
后退
顶部