程序中如何实现PING(100分)

  • 主题发起人 主题发起人 ses
  • 开始时间 开始时间
S

ses

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在程序启动时,PING一个地址,要是成功,那么有几个按钮就enable:=true,这要怎么做?
 
摘抄:1
uses winsock;

{-------------------------------------------------------------------------------}
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, 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;
 
摘抄2:
unit U_NET;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ComCtrls, ExtCtrls, ImgList;

type
TNetResourceArray=^TnetResource;
TF_NET = class(TForm)
StatusBar1: TStatusBar;
Panel1: TPanel;
TreeView1: TTreeView;
Panel2: TPanel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
Label1: TLabel;
Label2: TLabel;
ImageList1: TImageList;
procedure FormActivate(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private
procedure GetComputerName;
procedure GetGroupName;
{ Private declarations }
public

{ Public declarations }
end;

var
F_Net: TF_Net;
node: Ttreenode;

implementation

{$R *.DFM}

procedure TF_Net.FormActivate(Sender: TObject);
begin
node:=treeview1.Items.add(Treeview1.topitem, '整个网络');
node.imageindex:=0;
treeview1.SetFocus;
end;

procedure TF_Net.GetGroupName;
var
NetResource: TNetResource;
Buf: Pointer;
Count, BufSize, Res: DWORD;
lphEnum: THandle;
p: TNetResourceArray;
i, j: SmallInt;
NetworkTypeList: TList;
my_node_1:Ttreenode;
begin
statusbar1.panels[0].text := '正在列举域名...,请稍侯';
statusbar1.refresh;
NetworkTypeList := TList.Create;
//获取整个网络中的文件资源的句柄,lphEnum为返回名柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, nil, lphEnum);
if Res <> NO_ERROR then exit; //Raise Exception(Res);//执行失败
//获取整个网络中的网络类型信息
Count := $FFFFFFFF; //不限资源数目
BufSize := 8192; //缓冲区大小设置为8K
GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕
or (Res <> NO_ERROR) //执行失败
then Exit;
P := TNetResourceArray(Buf);
for I := 0 to Count - 1 do //记录各个网络类型的信息
begin
NetworkTypeList.Add(p);
Inc(P);
end;

//WNetCloseEnum关闭一个列举句柄
Res := WNetCloseEnum(lphEnum); //关闭一次列举
if Res <> NO_ERROR then exit;

for J := 0 to NetworkTypeList.Count - 1 do //列出各个网络类型中的所有工作组名称
begin //列出一个网络类型中的所有工作组名称
NetResource := TNetResource(NetworkTypeList.Items[J]^); //网络类型信息
//获取某个网络类型的文件资源的句柄,NetResource为网络类型信息,lphEnum为返回名柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource, lphEnum);
if Res <> NO_ERROR then break; //执行失败

while true do //列举一个网络类型的所有工作组的信息
begin
Count := $FFFFFFFF; //不限资源数目
BufSize := 8192; //缓冲区大小设置为8K
GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
//获取一个网络类型的文件资源信息,
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕
or (Res <> NO_ERROR) //执行失败
then break;
P := TNetResourceArray(Buf);
for I := 0 to Count - 1 do //列举各个工作组的信息
begin
my_node_1:=treeview1.Items.addchild(node, StrPAS(P^.lpRemoteName)); //取得一个工作组的名称
my_node_1.imageindex:=1;
Inc(P);
end;
end;
Res := WNetCloseEnum(lphEnum); //关闭一次列举
if Res <> NO_ERROR then break; //执行失败
end;
FreeMem(Buf);
NetworkTypeList.Destroy;
statusbar1.panels[0].text := '';
statusbar1.refresh;
statusbar1.panels[0].text := '';
statusbar1.refresh;
end;

procedure TF_Net.GetComputerName;
Var
NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp : TNetResourceArray;
groupname:string;
my_node:Ttreenode;
my_node_2:Ttreenode;
Begin
statusbar1.panels[0].text:='正在列举组名...,请稍侯';
statusbar1.refresh;
my_node:=treeview1.Selected;
groupname:= treeview1.Selected.Text;
FillChar(NetResource, SizeOf(NetResource), 0);//初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1];//指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;//类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK;//列举文件资源信息
//获取指定工作组的网络资源句柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> NO_ERROR Then Exit; //执行失败
While True Do//列举指定工作组的网络资源
Begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then break;//资源列举完毕
If (Res <> NO_ERROR) then Exit;//执行失败
Temp := TNetResourceArray(Buf);
For Ind := 0 to Count - 1 do//列举工作组的计算机名称
Begin
//获取工作组的计算机名称,+2表示删除"//",如//wangfajun=>wangfajun
my_node_2:=treeview1.Items.AddChild(my_node,Temp^.lpRemoteName + 2);
my_node_2.imageindex:=2;
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
If Res <> NO_ERROR Then exit;//执行失败
FreeMem(Buf);
statusbar1.panels[0].text:='';
statusbar1.refresh;

end;
procedure TF_NET.TreeView1Click(Sender: TObject);
begin
if treeview1.Selected.Level=0 then
begin
if treeview1.Selected.count=0 then
GetGroupName;
end;
if treeview1.selected.level=1 then
begin
if treeview1.Selected.count=0 then
GetComputerName;
end;
if treeview1.Selected.level=2 then
speedbutton1.Enabled:=true
else
speedbutton1.Enabled:=false
end;

procedure TF_NET.SpeedButton1Click(Sender: TObject);
begin
label1.Caption:=treeview1.Selected.Text;
label2.caption:='yes';
close;
end;

procedure TF_NET.SpeedButton2Click(Sender: TObject);
begin
close;
end;

end.
 
摘抄3:
使用icmp.dll
Type
TIPAddr = LongInt; // IP Address
TIPMask = LongInt; // An IP subnet mask.
TIPStatus = LongInt; // Status code returned from IP APIs.

PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation = packed record
TTL: Byte; // Time To Live (used for traceroute)
TOS: Byte; // Type Of Service (usually 0)
Flags: Byte; // IP header flags (usually 0)
OptionsSize: Byte; // Size of options data (usually 0, max 40)
OptionsData: PChar; // Options data buffer
end;

PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = record
Address : TIPAddr; // Replying address
Status : ULONG; // Reply IP_STATUS
RoundTripTime : ULONG; // RTT in milliseconds
DataSize : ULONG; // Reply data size in bytes
Reserved : ULONG; // Reserved for system use
Data : Pointer; // Pointer to the reply data
Options : PIPOptionInformation; // Reply options
end;

Function IcmpSendEcho(IcmpHandle : THandle;
DestinationAddress : TIPAddr;
RequestData : Pointer;
RequestSize : Word;
RequestOptions : PIPOptionInformation;
ReplyBuffer : Pointer;
ReplySize : DWord;
Timeout : DWord) : DWord; StdCall;
implementation

Const

IcmpDll = 'Icmp.dll';

Function IcmpCreateFile; External IcmpDll Name 'IcmpCreateFile';
Function IcmpCloseHandle; External IcmpDll Name 'IcmpCloseHandle';
Function IcmpSendEcho; External IcmpDll Name 'IcmpSendEcho';

------------------------------------------------------------------
errorcode := IcmpSendEcho(hICMP, FIPAddress, pReqData, FSize,
@IPOpt, pIPE, BufferSize, FTimeOut);
****既然你有了ics的ping控件,那么请看一下icmp.pas中对icmp.dll的写法。****
**** 以上所贴其实全是多余,ICS的icmp.pas中都有 *******
当你ping通一个IP地址后,
myhost:=gethostbyaddr(@FIPAddress,4,AF_INET);
hostname:=myhost.h_name;--》即得主机名。

windows下的gethostbyaddr的操作方式是:
先发包到dns中试图去获取主机名。如不成功则去取windows的主机名。一举两得。
 
我就一共收集了三个,呵呵。好像都没有实验,但是至少第一个应该可以[:D]
 
接受答案了.
 
后退
顶部