Delphi programming implements ping operation

zhaozj2021-02-08  244

Delphi programming implements ping operation

Zhang Tai Li

Users who use the network are familiar with the "ping" instruction, which is an executable file under DOS, which generally uses it to check the quality of the network connection. The basic principle is to use a function in the ICMP protocol in the TCP / IP protocol, i.e., send a request to the specified computer, and receive a request to return a response, to determine if the computer runs online or checks the network. Whether the connection is stable and reliable. During the implementation of the PING program, the resources taken by both parties have little resources, so it is a very practical tool.

We can implement "ping" operation by programming, improve it, so that it has Windows interface style, showing more intuitive than DOS.

First, a brief description of the dynamic link library required in the program: In the system directory of Windows, you can find the icmp.dll file, the dynamic link library provides all the features of the ICMP protocol, our programming is built Dynamic link library is called.

The call function in the icmp.dll file is as follows:

1, ICMPCREATEFILE

Open a handle and you can send ICMP requests to send messages through this handle.

2, ICMPCLOSEHANDLE

Close the handle you open through the IcmpCreateFile function.

3, ICMPsendecho

Send an ICMP request by the handle you open, returns after the timeout or answering message is received. Its parameters are basically consistent with its frame structure, refer to the following program part, which you can refer to books about ICMP protocols.

After first understanding the above three functions, we can start programming.

First, we should have the basic functions as shown in Figure 1 after running. To do this, we can first place the controls shown in the Delphi window, such as buttons, editing boxes, and text display boxes, etc.

(G72.jpg)

Routine routine

Then, in the beginning of the program, the Winsocket is initialized, and its function is the version information of the application, and the ICMP.DLL library is transferred.

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; Tmyping = Class (TFORM)

Panel1: TPANEL;

Label1: TLABEL;

Pingedit: tedit;

EXEBTN: TBUTTON

Button2: tbutton;

Button3: tbutton;

Statusshow: TMEMO;

Procedure Button3Click (Sender: TOBJECT);

Procedure formcreate (Sender: TOBJECT);

Procedure EXEBTNCLICK (Sender: TOBJECT);

Private

{Private Declarations}

HICMP: thandle;

ICMPCREATEFILE: TiCMpCreateFile;

ICMPCLOSEHANDLE: TicmpCloseHandle;

ICMPSENDECHO: TICMPSENDECHO;

public

{Public declarations}

END;

Procedure Tmyping.FormCreate (Sender: TOBJECT);

VAR

Wsadata: TWSADATA;

Hicmpdll: hmodule;

Begin

WSASTARTUP ($ 101, WSADATA);

// load the icmp.dll stuff

Hicmpdll: = loadingLibrary ('icmp.dll');

@ICMPCREATEFILE: = GetProcaddress (HiCMPDLL, 'ICMPCREATEFILE');

@IcmpcloseHandle: = getProcaddress (Hicmpdll, 'icmpCloseHandle');

@ICmpsendecho: = getProcadDress (Hicmpdll, 'Icmpsendecho');

HICMP: = ICMPCREATEFILE;

StatusShow.Text: = '';

StatusShow.Lines.Add ('Destination IP address byte number returns time (milliseconds)');

END;

Next, the actual programming process of the PING operation as shown below is performed.

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 (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 (PCHAR (PIPEDIT.TEXT) '' INTOSTR (PIPE ^ .datasize) '' INTOSTR (Pipe ^ .rt));

END;

FreeMem (prevdata);

FreeMem (PIPE);

end

END;

Through the above programming, we realize the interface operation of the PING function. In fact, there are still many features of the ICMP protocol, and can be implemented by the function call to ICMP.dll.

转载请注明原文地址:https://www.9cbs.com/read-1309.html

New Post(0)