zoukankan      html  css  js  c++  java
  • delphi 测试ping

    type
      TForm1 = class(TForm)
        idcmpclnt1: TIdIcmpClient;
        cxbtn1: TcxButton;
        cxbtn2: TcxButton;
        pnl1: TPanel;
        edtHost: TcxTextEdit;
        cxm1: TcxMemo;
        cxText_count: TcxTextEdit;
        procedure cxbtn1Click(Sender: TObject);
        procedure cxbtn2Click(Sender: TObject);
        procedure idcmpclnt1Reply(ASender: TComponent; const AReplyStatus: TReplyStatus);
      private
        { Private declarations }
        b, i, s: Integer;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.cxbtn1Click(Sender: TObject); //执行Ping操作
    begin
      b := 0; //掉包数量
      i := 0; //Ping的总次数
      s := 0; //执行(s=0)或停止(s=1)标志
      cxm1.Clear; //清除上次Ping的信息
    
      idcmpclnt1.Host := edtHost.Text;   //设置IP
      while (i < 100000) and (s = 0) do
      begin //最多执行10万次Ping操作
        Application.ProcessMessages;
        i := i + 1;
        idcmpclnt1.Ping; //执行Ping
       //若前10次Ping都失败则结束Ping
    
        if (i = 10) and (b = 10) then
          s := 1;
      end;
    end;
    
    procedure TForm1.cxbtn2Click(Sender: TObject); //停止Ping操作
    begin
      s := 1; //置Ping操作停止标志
    end;
    
    procedure TForm1.idcmpclnt1Reply(ASender: TComponent; const AReplyStatus: TReplyStatus);
    var
      Msg: string;
      Tm: integer;
    begin
      with AReplyStatus do
      begin
        Msg := 'Reply from ' + edtHost.Text;
        Msg := Msg + ' bytes=' + IntToStr(BytesReceived); //返回字节数
        Msg := Msg + ' TTL=' + IntToStr(TimeToLive); //返回生存时间
        Tm := MsRoundTripTime; //返回执行时间
    
        if Tm < 1 then
          Tm := 1;
        Msg := Msg + ' time=' + IntToStr(Tm) + 'ms';
        cxm1.Lines.Add(Msg); //保存信息
    
        if (BytesReceived = 0) or (TimeToLive = 0) then
        begin //无数据返回
          b := b + 1; //记录掉包数量
          cxText_count.text := IntToStr(b); //记录掉包数
        end;
    
        if i mod 100 = 0 then  //每Ping到100次显示一次掉包情况
          pnl1.Caption := 'Lost:' + IntToStr(b) + '/' + IntToStr(i) + '=' + copy(FloatToStr(b / i * 1000), 1, 4) + '';
    
      end;
    end;
  • 相关阅读:
    BZOJ3518 : 点组计数
    BZOJ2217 : [Poi2011]Lollipop
    李洪强经典面试题40-可能碰到的iOS笔试面试题-C语言
    对AFN的二次封装
    李洪强经典面试题39-iOS 程序员 6 级考试(答案和解释)
    iOS五种本地缓存数据方式
    iOS开发网络篇—数据缓存
    iOS中的通知
    李洪强漫谈iOS开发[C语言-048]-打印平方表
    李洪强漫谈iOS开发[C语言-047]-数列求和
  • 原文地址:https://www.cnblogs.com/yangxuming/p/8520696.html
Copyright © 2011-2022 走看看