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 常用4种对话框

Delphi 删除memo某一行

Delphi中case语法的使用方法

Delphi 判断字符串是否是单词

Delphi xe7android应用启用蓝牙

Delphi 获取当前目录下所有文件名3

Delphi superobject json操作类的基本用法

Delphi编程禁止用户关闭操作系统

Delphi获取其他进程中状态栏文本的函数

Delphi中进行指纹仪的二次开发

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



打赏

取消

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

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

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

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

评论

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