ping 应该是最好的办法了,除非你在每台机器上都装一个自己的客户端以外!
ping 例子
procedure TFindComputer.execute();
var
IPOpt:TIPOptionInformation;// IP Options for packet to send
FIPAddress
WORD;
pReqData,pRevData
Char;
pIPE
IcmpEchoReply;// ICMP Echo reply buffer
FSize: DWORD;
FTimeOut
WORD;
BufferSize
WORD;
ip:string;
MyString:string;
wsadata:twsadata;
p
hostent;
sip:string;
inetaddr:dword;
ListItem:TListItem;
i:integer;
Addbz:bool;
begin
DataModule1:=TDataModule1.Create(application);
while form1.CloseBZ=false do
begin
Form1.ProgressBar1.Position:=0;
DataModule1.ClientDataSet1.Close;
DataModule1.ClientDataSet1.Open;
Form1.ProgressBar1.Max:=DataModule1.ClientDataSet1.RecordCount;
while DataModule1.ClientDataSet1.Eof=false do
begin
Form1.ProgressBar1.Position:=Form1.ProgressBar1.Position+1;
ip:=DataModule1.ClientDataSet1.fieldbyname('IP').AsString;
form1.label1.Caption:='正在扫描:'+IP;
form1.update;
FIPAddress := inet_addr(PChar(ip));
FSize := 40;
MyString:='hello word';
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL := 64;
FTimeOut :=30;
form1.IcmpSendEcho(form1.hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE, BufferSize, FTimeOut);
try
try
if pReqData^ = pIPE^.Options.OptionsData^ then
begin
wsastartup(2,wsadata);
sip:=ip;
inetaddr:=inet_addr(pchar(sip));
try
p:=gethostbyaddr(@inetaddr,length(sip),pf_inet);
addbz:=true;
for i:=0 to Form1.ListView1.Items.Count-1 do
if Form1.ListView1.Items
.Caption=ip then
begin
addbz:=false;
Form1.ListView1.Items.SubItems[2]:=inttostr(strtoint(Form1.ListView1.Items.SubItems[2])+1);
if strtoint(Form1.ListView1.Items.SubItems[2])=2 then
Form1.ListView1.Items.ImageIndex:=1;
if strtoint(Form1.ListView1.Items.SubItems[2])>=3 then
Form1.ListView1.Items.ImageIndex:=2;
end;
if addbz=true then
begin
ListItem := Form1.ListView1.Items.Add;
ListItem.Caption :=ip;
ListItem.SubItems.Add(p^.h_name);
ListItem.SubItems.Add(Timetostr(time));
ListItem.SubItems.Add('1');
ListItem.ImageIndex:=0;
end;
finally
wsacleanup;
end;
end
else
begin
for i:=0 to Form1.ListView1.Items.Count-1 do
begin
if Form1.ListView1.Items.Caption=ip then
begin
Form1.ListView1.Items.Delete;
break;
end;
end;
end;
except
for i:=0 to Form1.ListView1.Items.Count-1 do
begin
if Form1.ListView1.Items.Caption=ip then
begin
Form1.ListView1.Items.Delete;
break;
end;
end;
end;
finally
FreeMem(pRevData);
FreeMem(pIPE);
end;
DataModule1.ClientDataSet1.Next;
end;
end;
end;