请给一个实现ping功能的代码。谢!(80分)

  • 主题发起人 主题发起人 netstorm
  • 开始时间 开始时间
N

netstorm

Unregistered / Unconfirmed
GUEST, unregistred user!
包括回复时间和ip地址
 
ics组件包中有包含此功能的组件,你要的话我发给你;
 
unit Mains;

interface

uses
Windows, Forms, winsock, StdCtrls, Classes,
Sysutils,Controls, ComCtrls, Buttons, Dialogs;

type
PIPOptionInformation= ^TIPOptionInformation;
TIPOptionInformation=packed record
TTL : Byte;
Tos : byte;
Flags : byte;
OptionsSize : Byte;
OptionsData : PChar;
end;

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;

TMainScreen = class(TForm)
StatusShow: TStatusBar;
BitBtn1: TBitBtn;
PingEdit: TEdit;
Label1: TLabel;
StatusText: TMemo;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);

private

HIcmp : THandle;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle: TIcmpCloseHandle;
ICmpSendEcho : TIcmpSendEcho;
hIcmpDll : HModule; // Load the icmp.dll stuff

public

end;

var
MainScreen : TMainScreen;

implementation

{$R *.dfm}

procedure TMainScreen.FormCreate(Sender: TObject);
var
WSAData : TWSAData;
begin
hICMPdll := LoadLibrary('Icmp.dll');
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
StatusShow.Panels[1].Text := '';
StatusShow.Panels[1].Text := '目的IP地址 字节数 返回时间(毫秒)';
//接下来,就要进行如下所示的Ping操作的实际编程过程了。
StatusText.Lines.Clear;
end;

procedure TMainScreen.BitBtn1Click(Sender: TObject);
var
IPOpt:TIPOptionInformation;// IP Options for packet to send   
FIPAddress:DWORD;
pReqData,pRevData:PChar;
pIPE:PIcmpEchoReply;// ICMP Echo reply buffer   
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
i : Integer;
begin

StatusText.Text := '';

if PingEdit.Text<>'' then
begin
try
FIPAddress := inet_addr(PChar(PingEdit.Text));
FSize := 200;
BufferSize := SizeOf(TICMPEchoReply) + FSize;

GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Hello,World,dddddddddddddddddddd'
+ 'dddddddddddddddddddddddddddddddd';

pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL := 64;
FTimeOut := 4000;
try
for I := 0 to 4 do
begin
IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),
@IPOpt, pIPE, BufferSize, FTimeOut);
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
StatusText.Lines.Add(
'Replay from ' + PChar(PingEdit.Text) + ' : bytes=' +
IntToStr(pIPE^.DataSize) + ' timeout=' + Trim(Format('%8.0f',[FTimeOut/1000])) +
'ms RTT='+IntToStr(pIPE^.RTT));
end;
end;
except
MessageDlg('Host is not existing.', mtConfirmation,[mbok],0);
end;
finally
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;
end;

procedure TMainScreen.FormDestroy(Sender: TObject);
begin
FreeLibrary(hIcmpDll);
end;

end.
 
Delphi里倒是自带了一个ping.pas,就是没用过。
不敢说好不好。
 
最近十分凑巧,我用两种方法实现了ping操作,呵呵
其一,用上述"直接法".
其二,可以直接利用系统中"ping"命令产生的结果回显.不过速度明显比第一种慢.
倒是很方便.而且只能发四个包,:)
 
是用shellapi吧
 
你 可 以 在 http://www.rtfm.be/fpiette/indexuk.htm下

载 Internet Component Suite, 这 是 个 免 费 的 Internet控 件 包 , 包 括 Ping功 能 , 并 提 供

了 全 部 源 程 序 。
 
我有一个在VC++下写的ping,已经是一年前写的了!
 
我写过DELPHI, C++BUILDER下的ping
 
API的看过一个VC的,没有自己写
ICS写过一个
 
sh630:
给我发一个bcb的ping吧!
 
楼上的e-mail address?
 
honglueyihzg@sina.com
 
to 飞来石:那个地址打不开:(
 


  首先,对编程中需要的动态链接库作一简要说明:在Windows的System目录下,你可以找到Icmp.dll文件,该动态链接库提供了ICMP协议的所有功能,我们的编程就建立在对该动态链接库的调用上。

  Icmp.dll文件内的调用函数说明如下:

  1、IcmpCreateFile

  打开一个句柄,通过该句柄你可以发送ICMP的请求回送报文。

  2、IcmpCloseHandle

  关闭你通过IcmpCreateFile函数打开的句柄。

  3、IcmpSendEcho

  通过你打开的句柄发送ICMP请求,在超时或应答报文接收后返回。其参数基本上和它的帧结构一致,可参看下面的程序部分,其具体含意你可以参看有关ICMP协议的书籍。

  初步了解了上述的三个函数后,我们就可以开始编程了。

  首先,我们的程序运行后应该有如图1所示的基本功能。为此,我们可先在Delphi的窗口中放入右上图中所示的控件,如按钮、编辑框和文本显示框等。

  (G72.JPG)

  例程运行示意图

  然后,在程序的开始部分(FormCreate)对WinSocket进行初始化,其作用是申明使用的版本信息,同时调入Icmp.dll库。

  type

   PIPOptionInformation = ^TIPOptionInformation;

   TIPOptionInformation = packed record

   TTL: Byte;

   TOS: Byte;

   Flags: Byte;

   OptionsSize: Byte;

   OptionsData: PChar;

   end;

   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;

   TMyPing = class(TForm)

   Panel1: TPanel;

   Label1: TLabel;

   PingEdit: TEdit;

   ExeBtn: TButton;

   Button2: TButton;

   Button3: TButton;

   StatusShow: TMemo;

   procedure Button3Click(Sender: TObject);

   procedure FormCreate(Sender: TObject);

   procedure ExeBtnClick(Sender: TObject);

   private

   { Private declarations }

   hICMP: THANDLE;

   IcmpCreateFile : TIcmpCreateFile;

   IcmpCloseHandle: TIcmpCloseHandle;

   IcmpSendEcho: TIcmpSendEcho;

   public

   { Public declarations }

  end;

  procedure TMyPing.FormCreate(Sender: TObject);

  var

   WSAData: TWSAData;

   hICMPdll: HMODULE;

  begin

   WSAStartup($101, WSAData);

   // Load the icmp.dll stuff

   hICMPdll := LoadLibrary('icmp.dll');

   @ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');

   @IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');

   @IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');

   hICMP := IcmpCreateFile;

   StatusShow.Text := '';

   StatusShow.Lines.Add('目的IP地址 字节数 返回时间(毫秒)');

  end;

  接下来,就要进行如下所示的Ping操作的实际编程过程了。

  procedure TMyPing.ExeBtnClick(Sender: TObject);

  var

   IPOpt:TIPOptionInformation;// IP Options for packet to send

   FIPAddress:DWORD;

   pReqData,pRevData:PChar;

   pIPE:PIcmpEchoReply;// ICMP Echo reply buffer

   FSize: DWORD;

   MyString:string;

   FTimeOut:DWORD;

   BufferSize:DWORD;

  begin

   if PingEdit.Text <> '' then

   begin

   FIPAddress := inet_addr(PChar(PingEdit.Text));

   FSize := 40;

   BufferSize := SizeOf(TICMPEchoReply) + FSize;

   GetMem(pRevData,FSize);

   GetMem(pIPE,BufferSize);

   FillChar(pIPE^, SizeOf(pIPE^), 0);

   pIPE^.Data := pRevData;

   MyString := 'Hello,World';

   pReqData := PChar(MyString);

   FillChar(IPOpt, Sizeof(IPOpt), 0);

   IPOpt.TTL := 64;

   FTimeOut := 4000;

   IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString), @IPOpt, pIPE, BufferSize, FTimeOut);

   if pReqData^ = pIPE^.Options.OptionsData^ then

   begin

   StatusShow.Lines.Add(PChar(PingEdit.Text) + ' ' +IntToStr(pIPE^.DataSize) + ' ' +IntToStr(pIPE^.RTT));

   end;

   FreeMem(pRevData);

   FreeMem(pIPE);

   end

  end;

  通过上面的编程,我们就实现了Ping功能的界面操作。实际上,ICMP协议的功能还有很多,都可以通过对Icmp.dll的函数调用来实现。

 
后退
顶部