如何能用代码实现检测本机和一个IP地址是通畅的。比如 是否能ping 通 www.sohu.com 

解决方案 »

  1.   

    安装第三方组件包:Indy,并使用TIdIcmpClient:    IdIcmpClient1.Host:= 'www.sohu.com';
        try
          IdIcmpClient1.Ping();
        except
          on e: exception do
            ShowMessage('与主机 www.sohu.com 的连接失败,请联系网管。');
        end;
      

  2.   

    uses
    IdBaseComponent, IdComponent, IdRawBase,
      IdRawClient, IdIcmpClient;
    function IsOnline(host:string):boolean;
    var
    IcmpClient:TIdIcmpClient;
    begin
    IcmpClient:=TIdIcmpClient.Create(nil);
    IcmpClient.BufferSize:=8192;
    IcmpClient.Port:=0;
    IcmpClient.Protocol:=1;
    IcmpClient.ReceiveTimeout:=5000;
    IcmpClient.Host:=host;
    try
      IcmpClient.Ping;
      result:=true;
      except
      result:=false;
      end;
    IcmpClient.Free;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    if isOnline('www.sohu.com') then
       showmessage('在线') else
       ShowMessage('不在线');
    end;
      

  3.   

    这个办法达不到我的效果,只要输入任何一个IP地址的格式 比如 www.weoiurwoieurewurewurowuerouewo.com 用上面的办法返回的结构都是true 除非输入数字 比如112等才会返回false  而我想要的效果是我能否ping 通一个IP地址,比如我输入 www.sohu12345678999.com 就应该返回false 请高手指点
      

  4.   

    在Indy组件中有域名转IP的转换应用.查一下