delphi用什么控件实现对客户机的ping呀?在哪个控件页里?(20分)

  • 主题发起人 主题发起人 代码雪儿
  • 开始时间 开始时间

代码雪儿

Unregistered / Unconfirmed
GUEST, unregistred user!
ping
想实现看看客户机通不通。
 
没有先成控件.是调用一个winsock.dll文件,其代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock, ExtCtrls;

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;
TTMyPing = class(TForm)
PingEdit: TEdit;
StatusShow: TMemo;
exebtn: TButton;
Panel1: TPanel;
StaticText1: TStaticText;
StaticText2: TStaticText;
procedure exebtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
hICMP: THANDLE;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle:TIcmpCloseHandle;
IcmpSendEcho: TIcmpSendEcho;
public
WSAData:TWSAData;
{ Public declarations }
end;

var
TMyPing: TTMyPing;

implementation

{$R *.DFM}

procedure TTMyPing.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);
try
try
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
StatusShow.Lines.Add(PChar(PingEdit.Text) + '-----'
+IntToStr(pIPE^.DataSize) + '-----' +IntToStr(pIPE^.RTT));
end;
except
showmessage('没有找到Ip地址!');
end;
finally
FreeMem(pRevData);
FreeMem(pIPE);
end;
end
else
showmessage('请输入Ip地址');
end;
//   通过上面的编程,我们就实现了Ping功能的界面操作。实际上,ICMP协议的功能还
//有很多,都可以通过对Icmp.dll的函数调用来实现。


procedure TTMyPing.FormCreate(Sender: TObject);
var
hICMPdll: HMODULE;
begin // Load the icmp.dll stuff
WSAStartup(2,WSAData);
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 TTMyPing.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WSACleanup();
end;

end.

 
D6的INDY里那个ICMP控件
 
ICS,去 http://overbyte.delphicenter.com/frame_index.html 下载.
 
用D6+INDY9不错。IDICMP控件不错,你可以在他的DEMOS里找到一个叫PING GUI的DEMO。
 
我看网上文章说用Ping控件,还起个名字叫Ping1,我怎么找不到,你们说的那些控件是吗?
 
http://www.torry.net/apps/utilities/network/xping.zip
 
你让我下个exe文件干吗?
 
Delphi 自己带有啊!Indy 页里面的 idIcmp 啊:
var
idIcmp: TIdIcmp;
begin
idIcmp := TIdIcmp.Create(nil);
idIcmp.OnReply := OnPingReply;
idIcmp.ReceiveTimeout := 500; // 超时设定
idIcmp.Host := 'www.163.net';
idIcmp.Ping;

procedure TForm1.OnPingReply(ASender: TComponent;
const AReplyStatus: TReplyStatus);
begin
if AReplyStatus.ReplyStatusType = rsEcho then
ShowMessage('速度:' + IntToStr(AReplyStatus.MsRoundTripTime));
else
ShowMessage('超时');
end;
 
IP*Works! 5 VCL (Ping Component)
http://www.nsoftware.com/products/controls/?ctl=Ping

 
引用IdIcmpClient

with TIdIcmpClient.Create(self) do
try
host:=getip(readurl());
onreply:=reply;
readstatus('正在连接远程服务器('+host+')...');
try
ping;
except
readstatus('地址('+host+')不可用或没有连接到Internet。');
end;
finally
free;
end;
reply事件
case areplystatus.ReplyStatusType of
rsEcho:begin
readstatus('远程服务器良好,可以开始传送。');
end;
rsError:readstatus('收到错误的回应信息。网络状况有问题。');
rsTimeOut:readstatus('服务器没有响应,超时了。');
rsErrorUnreachable:readstatus('错误的地址。');
rsErrorTTLExceeded:readstatus('TTL响应超时,网络状况很不好,建议不要传送。');
else
readstatus('其它未知错误。');
end;
其中readstatus是我自定义的现实错误的函数
 
billst得不错,调试成功
To:beta
我找不到你说得控件
To:other:
还有好几个地方不明白呀。
用D6+INDY9不错。IDICMP控件不错,------这个控件我找不到,在哪里?
要下载吗?
 
方法一
  首先,对编程中需要的动态链接库作一简要说明:在Windows的System目录下
,你可以找到Icmp.dll文件,该动态链接库提供了ICMP协议的所有功能,我们的
编程就建立在对该动态链接库的调用上。Icmp.dll文件内的调用函数说明如下:
  1、IcmpCreateFile   打开一个句柄,通过该句柄你可以发送ICMP的请求
回送报文。
  2、IcmpCloseHandle   关闭你通过IcmpCreateFile函数打开的句柄。
  3、IcmpSendEcho   通过你打开的句柄发送ICMP请求,在超时或应答报
文接收后返回。其参数基本上和它的帧结构一致,可参看下面的程序部分,其具

体含意你可以参看有关ICMP协议的书籍。
 初步了解了上述的三个函数后,我们就可以开始编程了。
  首先,我们的程序运行后应该有如图1所示的基本功能。为此,我们可先在
Delphi的窗口中放入右上图中所示的控件,如按钮、编辑框和文本显示框等。
 然后,在程序的开始部分(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:IPOptionInformation;
   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
   // 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,FSiz e);   
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的函数调用来实现。
 
方法二
文章:http://www.computerworld.com.cn/htm/app/salon/01_9_24_3.asp
控件:http://www.delphibyte.com/download/softview.php?softid=124
 
方法三:加分
 
美女好呀!
 
多人接受答案了。
 
后退
顶部