自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Delphi實(shí)現(xiàn)ICMP監(jiān)控的代碼

網(wǎng)絡(luò) 網(wǎng)絡(luò)管理
下面我們來介紹一下Delphi的ICMP的監(jiān)控狀態(tài)的具體實(shí)現(xiàn)代碼。那么對(duì)于代碼的含義我們做了具體的講解,希望對(duì)大家有所幫助。

遠(yuǎn)程監(jiān)控,是我們管理設(shè)置與網(wǎng)絡(luò)的有效措施,這一方法也引入到了越來越多的領(lǐng)域中。那么這里我們就來介紹一下Delphi中用ICMP探測(cè)遠(yuǎn)程主機(jī)狀態(tài)的具體方法。網(wǎng)絡(luò)通訊中經(jīng)常需要確定遠(yuǎn)程主機(jī)是否存活,以決定下一部進(jìn)行的操作??梢灾苯邮褂肐CMP協(xié)議來實(shí)現(xiàn),但是要考慮許多協(xié)議細(xì)節(jié),實(shí)現(xiàn)起來比較麻煩。Windows 自帶的ICMP庫里有現(xiàn)成的函數(shù)可以使用,只要在使用前填充相應(yīng)的數(shù)據(jù)結(jié)構(gòu)就可以了。以下是要使用的數(shù)據(jù)結(jié)構(gòu)。這些結(jié)構(gòu)MSDN里有C形式的聲明,這里給出的是Delphi的形式。

//用到的協(xié)議數(shù)據(jù)結(jié)構(gòu)
PIPOptionInfo = ^TIPOptionInfo; // IP 頭選項(xiàng)
TIPOptionInfo = packed record
TTL: Byte;//存活時(shí)間
TOS: Byte;//Type of Service,請(qǐng)求類型
Flags: Byte;//標(biāo)志
OptionsSize: Byte;//選項(xiàng)長度
OptionsData: PChar;//選項(xiàng)數(shù)據(jù)
end;
PIcmpEchoReply = ^TIcmpEchoReply;
TIcmpEchoReply = packed record // ICMP 返回信息
Address: DWORD;//IP地址
Status: DWORD;//狀態(tài)
RTT: DWORD;
DataSize: Word;//數(shù)據(jù)長度
Reserved: Word;//保留
Data: Pointer;//數(shù)據(jù)
Options: TIPOptionInfo;//選項(xiàng)區(qū)
end;
//動(dòng)態(tài)庫中的函數(shù)聲明
TIcmpCreateFile = function: THandle; stdcall; //創(chuàng)建ICMP句柄
TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall; //關(guān)閉ICMP句柄
TIcmpSendEcho = function(IcmpHandle:THandle; DestinationAddress:DWORD;
RequestData:Pointer; RequestSize:Word; RequestOptions:PIPOptionInfo;
ReplyBuffer:Pointer; ReplySize:DWord; Timeout:DWord):DWord; stdcall;//發(fā)送ICMP探測(cè)數(shù)據(jù)報(bào)
//要用到的變量聲明
hICMPDll,hICMP:THandle;
wsaData:TWSADATA;
ICMPCreateFile:TICMPCreateFile;
IcmpCloseHandle:TIcmpCloseHandle;
IcmpSendEcho:TIcmpSendEcho;
//destip:要探測(cè)的遠(yuǎn)程地址,形如 192.168.1.1
procedure f_CheckOnline(destip:string);
 var
  IPOpt:TIPOptionInfo;// 發(fā)包的 IP 選項(xiàng)
  IPAddr:DWORD;
  pReqData,pRevData:PChar;
  pIPE:PIcmpEchoReply;// ICMP Echo 回復(fù)緩沖區(qū)
  FSize: DWORD;
  MyString:string;
  FTimeOut:DWORD;
  BufferSize:DWORD;
  i:integer;
 begin
  hICMPdll := LoadLibrary('icmp.dll'); //調(diào)取icmp 動(dòng)態(tài)庫
  if hICMPDll<>NULL then
   begin
    WSAStartup($101,wsaData);//初始化網(wǎng)絡(luò)協(xié)議棧
    @ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile'); //取動(dòng)態(tài)庫中的導(dǎo)出函數(shù)
    @IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
    @IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
    hICMP := IcmpCreateFile; //創(chuàng)建 icmp句柄
    IPAddr:= inet_addr(PChar(destip)); //取要探測(cè)的遠(yuǎn)端主機(jī)ip地址
    FSize := 40;
    BufferSize := SizeOf(TICMPEchoReply) + FSize;
    GetMem(pRevData,FSize);
    GetMem(pIPE,BufferSize);
    FillChar(pIPE^, SizeOf(pIPE^), 0);
    pIPE^.Data := pRevData;
    MyString := 'Hi, OnLine?';//任意字符串
    pReqData := PChar(MyString);
    FillChar(IPOpt, Sizeof(IPOpt), 0);
    IPOpt.TTL := 64;
    FTimeOut := 500;//等待時(shí)長
    i:=IcmpSendEcho(hICMP, IPAddr, pReqData, Length(MyString), @IPOpt, pIPE, BufferSize, FTimeOut);//如果有返回,返回值表示收到的回復(fù)的個(gè)數(shù)。如果為0表示沒有回復(fù),主機(jī)無法到達(dá)
    FreeMem(pRevData);
    FreeMem(pIPE);
    IcmpCloseHandle(hicmp);
    FreeLibrary(hICMPdll);//釋放動(dòng)態(tài)庫
    WSAcleanup();//清理協(xié)議棧
  end;
end;

以上就是Delphi用ICMP探測(cè)遠(yuǎn)程主機(jī)狀態(tài)的具體代碼。

責(zé)任編輯:佟健 來源: 賽迪網(wǎng)
相關(guān)推薦

2010-07-13 10:38:54

2010-08-02 14:52:23

ICMP回顯

2010-08-02 14:36:52

ICMPLinux

2010-07-30 16:10:57

ICMPping

2010-08-02 14:29:46

LinuxPingICMP

2015-12-11 11:49:19

java

2015-12-11 11:39:15

.net代碼

2010-08-02 16:59:54

2010-07-13 10:10:07

2011-04-06 13:28:46

Delphi

2010-08-02 16:51:54

2010-07-30 16:17:53

2010-08-02 16:41:15

2010-07-30 16:00:36

ICMP協(xié)議

2012-05-07 14:11:30

Delphi

2010-06-13 15:53:35

ICMP協(xié)議

2010-08-02 16:14:54

2023-03-02 09:17:50

全鏈路監(jiān)控系統(tǒng)

2010-08-02 16:43:46

ICMP協(xié)議

2011-04-06 16:14:40

Delphi
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)