如何快速检测计算机是否上网?(150)

  • 主题发起人 主题发起人 xiuy0924
  • 开始时间 开始时间
X

xiuy0924

Unregistered / Unconfirmed
GUEST, unregistred user!
尝试了InternetGetConnectedState及InternetCheckConnection,的确可以判断,但是一旦网络异常断开时,程序就处于假死状态,如何能在不影响程序运行的情况下快速检测是否上网呢?包括通过路由器,拨号等方式上网
 
我也想知道。
 
访问一个地址,比如时间服务器,看是否返回数据或者看流量。
 
是不是可以把他放在TIMER时间里面啊,使他每过段时间,就自己检查下,是否在网上。
 
在需要连接网络的地方进行异常处理。
 
对于阻塞函数可以通过子线程执行来避免假死现象。
 
弹出msgbox询问是否在上网
 
多少年来大家都想解决这个问题,较好的办法就是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):Dword;stdcall; var IPOpt:TIPOptionInformation;// IP Options for packet to send pReqData,pRevData:PChar; FIPAddress,BufferSize,FTimeOut,FSize,Fstatus: Dword; pIPE:PIcmpEchoReply;// 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.
 
嗯 其实不用那么简单。用webbrowse访问baidu看看不就知道了。如果里面有数据就是成功的,只要你吧webbrowse隐藏就可以! 那么简单的问题!
 
还是用了Ping,代码和独孤客差不多,谢谢各位的指导
 
后退
顶部