Delphi跨平台检测网络连接状态


本文整理自网络,侵删。

 
功能
跨平台检测网络

环境
Delphi 10.3.3
Win10 64位
主要代码

uses IdTCPClient;
function CheckInternet(AHost:string='baidu.com';Aport:integer=80):Boolean;
var
 CIdtcpclient:TIdtcpclient;
begin
 Result:=False;
 try
   try
     CIdtcpclient:= TIdtcpclient.Create(nil);
     CIdtcpclient.ReadTimeout :=2000;
     CIdtcpclient.ConnectTimeout :=2000;
     CIdtcpclient.Port := Aport;
     CIdtcpclient.Host := AHost;
     CIdtcpclient.Connect;
     CIdtcpclient.Disconnect;
     Result:=True;
   except
      on e:Exception do
      begin
        Result:=False;
      end;
   end;
 finally
    CIdtcpclient.DisposeOf;
    CIdtcpclient:=nil;
 end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if CheckInternet then
    showmessage('网络已连接')
  else
  showmessage('网络未连接');
end;

相关阅读 >>

Delphi [函数] unicode 检查字符串是否含中文字

Delphi xe6 �c使用android的zlib

Delphi 主程序与dll之间的全局变量问题

文件占坑对付云安全

Delphi服务程序(service)的调试方法

Delphi 字符串加密与解密函数

Delphi unigui获取连接的客户端列表

Delphi showmodal与show的区别

Delphi 中文姓名 合法性检查

Delphi 在长文件名和短文件名之间转换

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...