网络检查是否连通?(50分)

  • 主题发起人 主题发起人 liujxing263
  • 开始时间 开始时间
L

liujxing263

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位高手,在DOS中Ping命令可以判断网络是否连通,但在Delphi中怎样判断一台计算机与其他计算机连通呢?
 
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 ComboBox1.Text <> '' then
begin
FIPAddress := inet_addr(PChar(ComboBox1.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
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
StatusShow.Lines.Add('IP Address bytes time(ms)');
StatusShow.Lines.Add(PChar(ComboBox1.Text) + ' ' +IntToStr(pIPE^.DataSize) + ' ' +IntToStr(pIPE^.RTT));
StatusShow.Lines.Add('Test Time:'+TimeToStr(now));
end;
except
begin
// StatusShow.Clear ;
StatusShow.Lines.Add('Address:'+ComboBox1.Text+' No Found;');
StatusShow.Lines.Add('Test Time:'+TimeToStr(now));
end;
// messagedlg('没有找到该IP地址!',mtinformation,[mbok],0);
end;
pIPE^.Phe := GetHostByAddr(@FIPAddress, 4, AF_INET);
if pIPE^.Phe <> Nil then HostName:=pIPE^.Phe^.h_name;
// caption:=hostname;

StatusShow.Lines.Add(hostname);
StatusShow.Lines.Add('');

FreeMem(pRevData);
FreeMem(pIPE);
end;
end;

======================================

或一个图型 PING 源代如下:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, 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;
phe: pHostent;
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;


TForm1 = class(TForm)
Panel1: TPanel;
pingedit: TEdit;
exebutton: TButton;
Label1: TLabel;
StatusShow: TMemo;
procedure FormCreate(Sender: TObject);
procedure exebuttonClick(Sender: TObject);
private
{ Private declarations }
hICMP: THANDLE;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle: TIcmpCloseHandle;
IcmpSendEcho: TIcmpSendEcho;
public
{ Public declarations }
end;

var
Form1: TForm1;
HostName:string;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
WSAData: TWSAData;
hICMPdll: HMODULE;
begin
wsastartup($101,wsadata);
hICMPdll := LoadLibrary('icmp.dll');
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
StatusShow.Text := '';
statusshow.Align := alclient;
StatusShow.Lines.Add('目的IP地址 字节数 返回时间(毫秒)');
end;

procedure TForm1.exebuttonClick(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
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
StatusShow.Lines.Add(PChar(PingEdit.Text) + ' ' +IntToStr(pIPE^.DataSize) + ' ' +IntToStr(pIPE^.RTT));
end;
except
messagedlg('没有找到该IP地址!',mtinformation,[mbok],0);
end;
pIPE^.Phe := GetHostByAddr(@FIPAddress, 4, AF_INET);
if pIPE^.Phe <> Nil then HostName:=pIPE^.Phe^.h_name;
caption:=hostname;
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;


end.
 
用Indy下组件,一句话搞定。
 
能详细一点吗?
 
Program Files/Borland/Delphi6/Demos/Indy/PingGUI
有demo
 
请问ICS中有个PING的控件在哪下载?我找了好多次都找不到,请大哥们帮帮馒忙呀!
 
接受答案了.
 

Similar threads

回复
0
查看
838
不得闲
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部