怎样判断网络是否通?附一种方法(100分)

  • 主题发起人 主题发起人 Theblackismelan
  • 开始时间 开始时间
T

Theblackismelan

Unregistered / Unconfirmed
GUEST, unregistred user!
想做个软件,可以直观的看到网内哪台电脑连接正常,哪台是断开。
希望大家多指教。
本人找了种方法
主要组件定义
组件名称 类型 主要属性 使用事件及方法
NetCheckForm TForm Menu=NetChech_Menu OnActivate=FormActivate
OnCloseQuery=FormCloseQuery
NetLibTable TTable TableName=IpLib.DB
NetLibDataSource TDataSource DataSet =NetLibTable
icmpping TidIcmpClient 方法 OnReply=icmppingReply
NetCheck_Timer TTimer OnTimer=Net_Check
2、监测过程。设置定时器定时自动在IP地址表中读入每个网点机的IP地址,更改TidIcmpClient控件的Host属性值,并调用其Ping()方法,发送请求报文和接收应答报文。其主要的代码段如下:
procedure TNetCheckForm.Net_Check(Sender:TObject);
begin
NetLibDataSource.DataSet.First;
NetlibDataSource.DataSet.Edit;
while not NetLibDataSource.DataSet.EOFdo
begin
TIpAddress:=NetLibDataSource.DataSet.FieldByName('节点IP').AsString;
//从IP库中取网点IP地址
NameString:=NetLibDataSource.DataSet.FieldByName('节点名称').AsString;
//从IP库中取网点名称
icmpping.Host:= TIpAddress;
try
icmpping.Ping;
Application.ProcessMessages ;
//延时
finally
end;
end;
NetCheckStatusBar.Panels[1].Text:=DateTimeToStr(Now);
end;

3、获取接收应答报文。调用TidIcmpClient控件Ping()方法后OnICMPReply事件触发,读取AreplyStatus对象判断联通性。其主要的代码段如下:
procedure TNetCheckForm.icmppingReply(ASender: TComponent;
const AReplyStatus: TReplyStatus);
begin
NetlibDataSource.DataSet.Edit;
TIpAddress:=NetLibDataSource.DataSet.FieldByName('节点IP').AsString;
//从IP库中取节点IP地址
NameString:=NetLibDataSource.DataSet.FieldByName('节点名称').AsString;
//从IP库中取节点名称
if AReplyStatus.BytesReceived <> 0 then
//判断回波并给出网络结果
begin
NetLibDataSource.DataSet.FieldByName('状态').AsString:='正常';
CreateNSLog(DateTimeToStr(Now)+' '+ NameString+':['+TIpAddress+'] 连接正常');
end
else
begin
NetLibDataSource.DataSet.FieldByName('状态').AsString:='中断';
CreateNSLog(DateTimeToStr(Now)+' '+ NameString+':['+TIpAddress+'] 连接中断');
end;
NetLibDataSource.DataSet.Next;
NetlibDataSource.DataSet.Edit;
end;

4、日志文件的生成。日志文件为文本文件每月一个。其主要的代码段如下:
procedure CreateNSLog(S:String);
var
NSLog:String;
TFile:TextFile;
begin
NSLog:='NS'+ YearMonthString + '.LOG';
if FileExists(NSLog) then
begin
AssignFile(TFile,NSLog);
Append(TFile);
end
else
begin
AssignFile(TFile,NSLog);
Rewrite(TFile);
end;
Writeln(TFile,S);
CloseFile(TFile);
end;
可能太乱,麻烦了,我就是找不到上面所说的那些组件。谁能告诉我这些组件哪找,或者更好的办法,谢谢大家了
 
懂的高手别吝啬撒,还是说我的问题比较低级,懒的回答,都是从菜鸟升上去的哇!
或者对我上面说的方法给点意见也可以啊~~~~[:(]
 
unit ping;
interface
uses
Windows, SysUtils, Classes, Controls, Winsock;
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;
Tping =class(Tobject)
private
{ Private declarations }
hICMP: THANDLE;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle: TIcmpCloseHandle;
IcmpSendEcho: TIcmpSendEcho;
public
procedure pinghost(ip:string;var info:string);
constructor create;
destructor destroy;override;
{ Public declarations }
end;

var
hICMPdll: HMODULE;
implementation
constructor Tping.create;
begin
inherited create;
hICMPdll := LoadLibrary('icmp.dll');
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll,'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
end;

destructor Tping.destroy;
begin
FreeLibrary(hIcmpDll);
inherited destroy;
end;

procedure Tping.pinghost(ip:string;var info:string);
var
// IP Options for packet to send
IPOpt:TIPOptionInformation;
FIPAddress:DWORD;
pReqData,pRevData:PChar;
// ICMP Echo reply buffer
pIPE:PIcmpEchoReply;
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
begin
if ip <> '' then
begin
FIPAddress := inet_addr(PChar(ip));
FSize := 40;
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Test Net';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL := 64;
FTimeOut := 4000;
try
IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE, BufferSize, FTimeOut);
if pReqData^ = pIPE^.Options.OptionsData^ then
info:=ip+ ' ' + IntToStr(pIPE^.DataSize) + '   ' +IntToStr(pIPE^.RTT);
except
info:='Can not find host!';
FreeMem(pRevData);
FreeMem(pIPE);
Exit;
end;
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;

end.
 
谢谢你的回答,能不能把你做的这个东西发来研究下呢#17,如果可以的话发我邮箱吧,十分感谢,我邮箱是ay_libing@yahoo.com.cn 谢谢你了
 
接受答案了.
 

Similar threads

后退
顶部