delphi如何能判断网络通不通?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi如何能判断网络通不通?相关的知识,希望对你有一定的参考价值。
用DELPHI做了个小程序,自动打开一个网站。例如:
ShellExecute(Application.Handle, nil,'http://www.baidu.com', nil, nil, SW_SHOWNORMAL);
我想实现一个效果,就是当电脑网络不通(例如掉网线)或者网站本身打不开时,软件中提示“网站无法打开,请检查网络或网站”;这样怎么实现?
//---------------
//公用函数
//引用操作系统SensApi.dll 判断当前的网络是否连接
function IsNetworkAlive(var lpdwFlagsLib:Integer):Integer;stdcall;external'SensApi.dll';
//ping网络
function fucPing(url: String): Boolean;
//获取当前网络的连接状态 add by jzh 2010-05-24
function fucIsNetworkAlive: Boolean;
const
NETWORK_ALIVE_LAN = 1; //通过局域网上网
const
NETWORK_ALIVE_WAN = 2; //通过广域网上网
var
falg: Integer;
bAlive: Boolean;
begin
try
bAlive:= False;
IsNetworkAlive(falg);
case falg of
NETWORK_ALIVE_LAN:
begin
bAlive:= True;
end;
NETWORK_ALIVE_WAN:
begin
bAlive:= True;
end;
end;
result:= bAlive;
except
result:= false;
end;
end;
//ping网络
function fucPing(url: String): Boolean;
var
aIdICMPClient: TIdICMPClient;
begin
aIdICMPClient:= TIdICMPClient.Create(nil);
aIdIcmpclient.ReceiveTimeout:=500;
aIdICMPClient.Host:= url;
try
aIdICMPClient.Ping();
except
Result:= false;
end;
if (aidicmpclient.ReplyStatus.fromipaddress<>'0.0.0.0')
and (aidicmpclient.ReplyStatus.fromipaddress<>'') then
result:= true
else
result:= false;
aIdICMPClient.Free;
end; 参考技术B uses WinInet;
// 判断是否连网(网线是否被拔出)
function ISInternetConnected():Boolean;
const
MODEM = 1;
LAN = 2;
PROXY = 4;
MODEM_BUSY = 8;
var
dwConnectionTypes:DWORD;
begin
dwConnectionTypes:= MODEM + LAN + PROXY;
Result:=InternetGetConnectedState(@dwConnectionTypes,0);
end;本回答被提问者和网友采纳 参考技术C ping www.baidu.com
至于怎么ping,网上有很多函数或控件 参考技术D 用delphi实现ping
函数
procedure pinghost(ip:string;var info:string);
ip:目标IP地址;
info:ping了以后产生的信息(1)或(2);
(1)成功信息
ip 发送测试的字符数 返回时间
(2)出错信息
Can not find host!
使用
uses ping;
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
ping:Tping;
begin
ping:=Tping.create ;//一定要初试化哦
ping.pinghost('127.0.0.1',str);
memo1.Lines.Add(str);
ping.destroy ;
end;
[ping.pas]
(*作者:e梦缘*)
unit ping;
interface
uses
Windows, SysUtils, Classes, Controls, Winsock,
StdCtrls;
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;
Tping =class(Tobject)
private
Private declarations
hICMP: THANDLE;
IcmpCreateFile : TIcmpCreateFile;
IcmpCloseHandle: TIcmpCloseHandle;
IcmpSendEcho: TIcmpSendEcho;
public
procedure pinghost(ip:string;var info:string);
constructor create;
destructor destroy;override;
Public declarations
end;
var
hICMPdll: HMODULE;
implementation
constructor Tping.create;
begin
inherited create;
hICMPdll := LoadLibrary('icmp.dll');
@ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
@IcmpCloseHandle := GetProcAddress(hICMPdll,'IcmpCloseHandle');
@IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
hICMP := IcmpCreateFile;
end;
destructor Tping.destroy;
begin
FreeLibrary(hIcmpDll);
inherited destroy;
end;
procedure Tping.pinghost(ip:string;var info:string);
var
// IP Options for packet to send
IPOpt:TIPOptionInformation;
FIPAddress:DWORD;
pReqData,pRevData:PChar;
// ICMP Echo reply buffer
pIPE:PIcmpEchoReply;
FSize: DWORD;
MyString:string;
FTimeOut:DWORD;
BufferSize:DWORD;
begin
if ip <> '' then
begin
FIPAddress := inet_addr(PChar(ip));
FSize := 40;
BufferSize := SizeOf(TICMPEchoReply) + FSize;
GetMem(pRevData,FSize);
GetMem(pIPE,BufferSize);
FillChar(pIPE^, SizeOf(pIPE^), 0);
pIPE^.Data := pRevData;
MyString := 'Test Net - Sos Admin';
pReqData := PChar(MyString);
FillChar(IPOpt, Sizeof(IPOpt), 0);
IPOpt.TTL := 64;
FTimeOut := 4000;
try
IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE, BufferSize, FTimeOut);
if pReqData^ = pIPE^.Options.OptionsData^ then
info:=ip+ ' ' + IntToStr(pIPE^.DataSize) + ' ' +IntToStr(pIPE^.RTT);
except
info:='Can not find host!';
FreeMem(pRevData);
FreeMem(pIPE);
Exit;
end;
FreeMem(pRevData);
FreeMem(pIPE);
end;
end;
end.
1883端口拼不通
参考技术A 电脑防火墙没开1883端。在“控制面板”里点击“启用或关闭WindowsDefender防火墙”,将WindowsDefender防火墙启用。
Android应用端连接需要被访问端口时,需要配置安全组主动开放端口。
以上是关于delphi如何能判断网络通不通?的主要内容,如果未能解决你的问题,请参考以下文章