多少年来大家都想解决这个问题,较好的办法就是ping,虽然麻烦了一点,还是最有保障的ping百度或Google就OK了。以下是ping的dll的源代码请自行修改或扩展其功能 library Connect; uses SysUtils,Classes, Windows,Variants,winsock; const icmpDLL = 'icmp.dll'; 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; var hICMP: Thandle; hICMPlib: HMODULE; IcmpCreateFile: TIcmpCreateFile; IcmpCloseHandle: TIcmpCloseHandle; IcmpSendEcho: TIcmpSendEcho; function Icmpinitialization:Boolean;stdcall; var WsData: TWSADATA; WVersionRequested: word; begin WVersionRequested := MakeWord(2,0); if WsaStartUp(wVersionRequested,WsData)= Socket_Error then begin Result := false; WSACleanup; Exit; end; hICMPlib := loadlibrary(icmpDLL); if hICMPlib <> null then begin @IcmpCreateFile := GetProcAddress(hICMPlib,Pchar('IcmpCreateFile')); @IcmpCloseHandle := GetProcAddress(hICMPlib,Pchar('IcmpCloseHandle')); @IcmpSendEcho := GetProcAddress(hICMPlib,Pchar('IcmpSendEcho')); end; if (@IcmpCreateFile=nil) or (@IcmpCloseHandle=nil) or (@IcmpSendEcho=nil) then begin Result := false; exit; end; hICMP := IcmpCreateFile; if hICMP = INVALID_HANDLE_VALUE then begin Result := false; Exit; end; Result := true; end; function NetPing(NetIP: String)
word;stdcall; var IPOpt:TIPOptionInformation;// IP Options for packet to send pReqData,pRevData
Char; FIPAddress,BufferSize,FTimeOut,FSize,Fstatus: Dword; pIPE
IcmpEchoReply;// ICMP Echo reply buffer MyString,HostIP:string; begin FIPAddress := inet_addr(Pchar(NetIP)); FSize :=40; BufferSize := SizeOf(TICMPEchoReply) + FSize; GetMem(pRevData,FSize); GetMem(pIPE,BufferSize); FillChar(pIPE^, SizeOf(pIPE^), $AA); pIPE^.Data := pRevData; MyString := 'Hello,Hello'; pReqData := PChar(MyString); FillChar(IPOpt, Sizeof(IPOpt), 0); IPOpt.TTL := 64; FTimeOut := 1500; Fstatus :=IcmpSendEcho(hICMP,FIPAddress, pReqData, Length(MyString), @IPOpt, pIPE, BufferSize, FTimeOut); if Fstatus = 0 then Result :=0 else begin HostIP := StrPas(inet_ntoa(TInAddr(pIPE^.Address))); if trim(HostIP)=trim(NetIP) then Result := 1//返回成功 else Result := 0;//数据包丢失 end; FreeMem(pRevData); FreeMem(pIPE); end; exports NetPing,Icmpinitialization; begin hICMP :=0; hICMPlib := 0; end.