program _ping; // simple ping program, using neither Indy nor icmp.dll, which doesnt give back // the round trip time. // author: retnyg @ http://krazz.net/retnyg {$APPTYPE CONSOLE} //{$define KOL} uses {$ifdef KOL} kol, {$endif} winsock, windows; function GetIPAddress(const HostName: string): string; // from JCL var R: Integer; WSAData: TWSAData; HostEnt: PHostEnt; Host: string; SockAddr: TSockAddrIn; begin Result := ''; R := WSAStartup($0101, WSAData); if R = 0 then try Host := HostName; if Host = '' then begin SetLength(Host, MAX_PATH); GetHostName(@Host[1], MAX_PATH); end; HostEnt := GetHostByName(@Host[1]); if HostEnt <> nil then begin SockAddr.sin_addr.S_addr := Longint(PLongint(HostEnt^.h_addr_list^)^); Result := inet_ntoa(SockAddr.sin_addr); end; finally WSACleanup; end; end; function GetRTTAndHopCount(DestIpAddress:DWORD; HopCount :pointer; MaxHops: DWORD; RTT : pointer):boolean; stdcall; external 'iphlpapi.dll'; function ping(host:string; var hopCount, RTT:DWORD; var ipAd:string):DWORD; // by retnyg // returns 0 is successfully, otherwise errorcode var ip: DWORD; begin result:=0; hopCount:=0; RTT:=0; ipAd := GetIPAddress(host); ip := inet_addr(@ipAd[1]); if not GetRTTAndHopCount(ip, @hopCount, 30, @RTT) then result:=GetLastError; end; var host,ipad:string; kewlline: string[80]; i:byte; code: dword; hopCount:dword; RTT:dword; finished:boolean=false; tid:dword; procedure progress; begin while not finished do begin write('.'); sleep(100); end; end; begin {$ifdef KOL} useinputoutput; {$endif} writeln('simple ping application by retnyg'); setlength(kewlline,80); for i:=1 to 80 do kewlline[i]:=#205; writeln(kewlline); if paramcount > 0 then begin host:=paramstr(1); write('pingin host : ' + host + ' '); createthread(0,0,@progress,nil,0,tid); code := ping(host,hopcount,rtt,ipad); finished:=true; if code = 0 then begin writeln(' success!'); if host <> ipAd then writeln('ipAddr : '+ipAd); writeln('hops : ', hopCount); writeln('roundtrip time : ', RTT, ' ms.'); end else writeln(#13#10'Error: ', code); end else writeln('syntax: ping [hostname/ipadress]'); readln; end.