Using ICMP to detect remote host status in Delphi

Source: Internet
Author: User
Tags reserved

In network communications, it is often necessary to determine whether a remote host survives to determine the next operation. Can be implemented directly using ICMP protocol, but to consider a lot of protocol details, to achieve more cumbersome. There are ready-made functions available in Windows ' own ICMP library, so just fill in the appropriate data structure before using it.

The following is the data structure to use. These structures MSDN has a C form of the statement, here is the form of Delphi.

//Used protocol data structure


pipoptioninfo = ^tipoptioninfo; IP Header Option


tipoptioninfo = packed record


ttl:byte;//Survival Time


Tos:byte;//type of Service, request type


flags:byte;//logo


optionssize:byte;//Option Length


optionsdata:pchar;//Option Data


end;


picmpechoreply = ^ticmpechoreply;


ticmpechoreply = packed record//ICMP return information


ADDRESS:DWORD;//IP Address


status:dword;//State


Rtt:dword;


datasize:word;//Data Length


reserved:word;//reserved


data:pointer;//Data


options:tipoptioninfo;//Option Area


end;

function declarations in
//dynamic Libraries


ticmpcreatefile = Function:thandle; stdcall; Create ICMP handle


Ticmpclosehandle = function (icmphandle:thandle): Boolean; stdcall; Close ICMP handle


Ticmpsendecho = function (icmphandle:thandle; Destinationaddress:dword;


Requestdata:pointer; Requestsize:word; Requestoptions:pipoptioninfo;


Replybuffer:pointer; Replysize:dword; Timeout:dword):D Word; stdcall;//send ICMP probing datagram


//variable declaration to be used


Hicmpdll,hicmp:thandle;


Wsadata:twsadata;


Icmpcreatefile:ticmpcreatefile;


Icmpclosehandle:ticmpclosehandle;


Icmpsendecho:ticmpsendecho;


//destip: Remote address to probe, shaped like 192.168.1.1


procedure F_checkonline (destip:string);


var

IP options for the
ipopt:tipoptioninfo;//contract


Ipaddr:dword;


Preqdata,prevdata:pchar;


pipe:picmpechoreply;//ICMP Echo Recovery buffer


Fsize:dword;


mystring:string;


Ftimeout:dword;


Buffersize:dword;


I:integer;


begin


Hicmpdll: = LoadLibrary (' Icmp.dll '); Fetch ICMP Dynamic library


if Hicmpdll<>null then


begin


WSAStartup ($101,wsadata);//Initialize network protocol stack


@ICMPCreateFile: = GetProcAddress (Hicmpdll, ' icmpcreatefile '); Take the exported function in the dynamic library


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


@IcmpSendEcho: = GetProcAddress (Hicmpdll, ' Icmpsendecho ');


hicmp: = Icmpcreatefile; Create ICMP handle


ipaddr:= inet_addr (Pchar (Destip)); The remote host IP address to be probed


fsize: = 40;


buffersize: = SizeOf (ticmpechoreply) + fsize;


Getmem (prevdata,fsize);


Getmem (pipe,buffersize);


Fillchar (pipe^, SizeOf (pipe^), 0);


pipe^. Data: = Prevdata;


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


Preqdata: = Pchar (MyString);


Fillchar (ipopt, Sizeof (ipopt), 0);


Ipopt.ttl: = 64;


ftimeout: = 500;//Wait Long


I:=icmpsendecho (hicmp, ipaddr, Preqdata, Length (MyString), @IPOpt, PIPE, buffersize, ftimeout);/If there is a return, the return value indicates the received reply Number of. If 0 indicates no reply, the host cannot reach


Freemem (Prevdata);


Freemem (PIPE);


Icmpclosehandle (HICMP);


FreeLibrary (hicmpdll)/free Dynamic library


WSACleanup ()//cleaning protocol stack


end;


end;

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.