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环境下基于spcomm控件开发串口通讯报文字节丢失的问题解决

Delphi 自带的字符串分割函数

Delphi 磁盘类型 getdrivetype

Delphi中emptyparam参数被改写问题

Delphi if语法,弹出提示框,不等于写法,判断是否为空

Delphi 提取字符串左侧内容

Delphi 备份恢复剪切板

Delphi实现win10下Delphi 10.3.1 inline hook 调试器法获取寄存器并修改

Delphi xe5实现按android的back键无法退出关闭程序

Delphi xe 取得 app 自己的版本号 (狠跨 4 个平台)

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



打赏

取消

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

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

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

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

评论

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