Delphi programming to implement Ping operation
Zhang Taili
Users who have used the Internet are familiar with the "Ping" command. It is an executable file under DOS. It is generally used 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 package, that is, to send a request to the specified computer, and the computer that receives the request returns a response to determine whether the computer is running on the Internet or to check the network. Is the connection stable and reliable? During the execution of the Ping program, both computers consume very little resources, so it is a very practical tool.
We can implement the "Ping" operation through programming and improve it so that it has a Windows interface style and the display is more intuitive than DOS.
First, a brief explanation of the dynamic link library needed in programming: In the System directory of Windows, you can find the Icmp.dll file. This dynamic link library provides all the functions of the ICMP protocol. Our programming is based on this On the call of dynamic link library.
The calling functions in the Icmp.dll file are described as follows:
1. IcmpCreateFile
Opens a handle through which you can send ICMP request echo messages.
2. IcmpCloseHandle
Close the handle you opened through the IcmpCreateFile function.
3. IcmpSendEcho
Send an ICMP request through the handle you opened and return after timeout or response message reception. Its parameters are basically consistent with its frame structure. Please refer to the program part below. For its specific meaning, you can refer to books on the ICMP protocol.
After a preliminary understanding of the above three functions, we can start programming.
First, our program should have the basic functions shown in Figure 1 after running. To do this, we can first put the controls shown in the upper right picture in the Delphi window, such as buttons, edit boxes, and text display boxes.
(G72.JPG)
Schematic diagram of routine operation
Then, WinSocket is initialized at the beginning of the program (FormCreate). Its function is to declare the version information used and at the same time call the Icmp.dll library.
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 := LoadLibrary('icmp.dll');
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
StatusShow.Text := ';
StatusShow.Lines.Add('Destination IP address bytes return time (milliseconds)');
end;
Next, it is time to carry out the actual programming process of the Ping operation as shown below.
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(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(PingEdit.Text) + ' ' +IntToStr(pIPE^.DataSize) + ' ' +IntToStr(pIPE^.RTT));
end;
FreeMem(pRevData);
FreeMem(pIPE);
end
end;
Through the above programming, we have realized the interface operation of the Ping function. In fact, there are many functions of the ICMP protocol, which can be realized through function calls to Icmp.dll.