delphi如何能判断网络通不通?

用DELPHI做了个小程序,自动打开一个网站。例如:ShellExecute(Application.Handle,nil,'http://www.baidu.com',... 用DELPHI做了个小程序,自动打开一个网站。例如:
ShellExecute(Application.Handle, nil,'http://www.baidu.com', nil, nil, SW_SHOWNORMAL);

我想实现一个效果,就是当电脑网络不通(例如掉网线)或者网站本身打不开时,软件中提示“网站无法打开,请检查网络或网站”;这样怎么实现?
展开
 我来答
bluedn
2010-11-16 · TA获得超过744个赞
知道小有建树答主
回答量:565
采纳率:0%
帮助的人:719万
展开全部
给你个我自己用的函数吧。

//---------------
//公用函数
//引用操作系统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;
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
renzhumin2
推荐于2018-05-06 · TA获得超过163个赞
知道小有建树答主
回答量:173
采纳率:0%
帮助的人:274万
展开全部
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;
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2010-11-16
展开全部
ping www.baidu.com

至于怎么ping,网上有很多函数或控件
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jjm1227
2010-11-16 · TA获得超过462个赞
知道小有建树答主
回答量:463
采纳率:0%
帮助的人:377万
展开全部
用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.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式