Code for implementing ICMP monitoring using Delphi

Source: Internet
Author: User
Article Title: code for implementing ICMP monitoring using Delphi. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Remote monitoring is an effective measure for us to manage settings and networks. This method has been introduced into more and more fields. Here we will introduce how to use ICMP to detect the status of remote hosts in Delphi. In network communication, you often need to determine whether the remote host is alive to determine the next operation. You can directly use the ICMP protocol for implementation, but you need to consider a lot of protocol details, it is more difficult to implement. There are ready-made functions available in the ICMP library that comes with Windows. You only need to fill in the corresponding data structure before use. The following is the data structure to use. The C-form statements are provided in MSDN. Here, the Delphi statements are provided.

// The Protocol Data Structure Used

PIPOptionInfo = ^ TIPOptionInfo; // IP header Option

TIPOptionInfo = packed record

TTL: Byte; // TTL

TOS: Byte; // Type of Service, request Type

Flags: Byte; // flag

OptionsSize: Byte; // Option Length

OptionsData: PChar; // option data

End;

PIcmpEchoReply = ^ TIcmpEchoReply;

TIcmpEchoReply = packed record // ICMP returned information

Address: DWORD; // ip Address

Status: DWORD; // Status

RTT: DWORD;

DataSize: Word; // Data Length

Reserved: Word; // Reserved

Data: Pointer; // Data

Options: TIPOptionInfo; // option Area

End;

// Function declaration in the dynamic library

TIcmpCreateFile = function: THandle; stdcall; // create an ICMP handle

TIcmpCloseHandle = function (IcmpHandle: THandle): Boolean; stdcall; // close the ICMP handle

TIcmpSendEcho = function (IcmpHandle: THandle; DestinationAddress: DWORD;

RequestData: Pointer; RequestSize: Word; RequestOptions: PIPOptionInfo;

ReplyBuffer: Pointer; ReplySize: DWord; Timeout: DWord): DWord; stdcall; // send ICMP detection Datagram

// Variable declaration to be used

HICMPDll, hICMP: THandle;

WsaData: TWSADATA;

ICMPCreateFile: TICMPCreateFile;

IcmpCloseHandle: TIcmpCloseHandle;

IcmpSendEcho: TIcmpSendEcho;

// Destip: the remote address to be detected, such as 192.168.1.1

Procedure f_CheckOnline (destip: string );

Var

IPOpt: TIPOptionInfo; // IP address option for packet sending

IPAddr: DWORD;

PReqData, pRevData: PChar;

PIPE: PIcmpEchoReply; // ICMP Echo reply Buffer

FSize: DWORD;

MyString: string;

FTimeOut: DWORD;

BufferSize: DWORD;

I: integer;

Begin

HICMPdll: = LoadLibrary ('ICMP. dll '); // retrieves the icmp dynamic library

If hICMPDll <> NULL then

Begin

WSAStartup ($101, wsaData); // initialize the network protocol stack

@ ICMPCreateFile: = GetProcAddress (hICMPdll, 'icmpcreatefile'); // obtain the export function from the dynamic library

@ IcmpCloseHandle: = GetProcAddress (hICMPdll, 'icmpclosehandle ');

@ IcmpSendEcho: = GetProcAddress (hICMPdll, 'icmpsendecho ');

HICMP: = IcmpCreateFile; // create an icmp handle

IPAddr: = inet_addr (PChar (destip); // obtain the IP address of the remote host to be tested.

FSize: = 40;

BufferSize: = SizeOf (TICMPEchoReply) + FSize;

GetMem (pRevData, FSize );

GetMem (pIPE, BufferSize );

FillChar (pIPE ^, SizeOf (pIPE ^), 0 );

PIPE ^. Data: = pRevData;

MyString: = 'Hi, OnLine? '; // Any string

PReqData: = PChar (MyString );

FillChar (IPOpt, Sizeof (IPOpt), 0 );

IPOpt. TTL: = 64;

FTimeOut: = 500; // waiting duration

I: = IcmpSendEcho (hICMP, IPAddr, pReqData, Length (MyString), @ IPOpt, pIPE, BufferSize, FTimeOut); // If yes, the return value indicates the number of replies received. If the value is 0, no response is returned and the host cannot arrive.

FreeMem (pRevData );

FreeMem (pIPE );

IcmpCloseHandle (hicmp );

FreeLibrary (hICMPdll); // release the dynamic library

WSAcleanup (); // clear the protocol stack

End;

End;

The code above shows how Delphi uses ICMP to detect the status of a remote host.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.