• C++代码读取ping的网络延时


    利用C++代码实现读取ping工具的网络延时数据

    直接上代码

    #include <QCoreApplication>
    #include <QTextCodec>
    #include <QProcess>
    #include <iostream>
    #include <Windows.h>
    
    
    void checkOnline( QString ip )
    {
      QProcess exc;
      QTextCodec* codec = QTextCodec::codecForName( "GBK" );
      QString cmdstr = "ping " + ip; //ping 192.168.15.90  -n 2 -w 4000
      while ( true )
      {
        exc.start( cmdstr ); //执行ping
        exc.waitForFinished( -1 ); //等待ping完成
        QString  outstr = codec->toUnicode( exc.readAll() ); //获取ping结果
        //返回不等于-1,说明ping结果包含"往返行程的估计时间"字段,则说明ping成功,网络可达;等于-1,表示没有此字段,说明ping不通
        QString test = "ms";
        int flag = outstr.indexOf( test );
        if ( ( -1 != flag ) )
        {
          int start_num = flag - 1;
          while ( true )
          {
            if ( outstr.mid( start_num - 1, 1 ) == "<" || outstr.mid( start_num - 1, 1 ) == "=" )
            {
              break;
            }
            start_num --;
          }
          QString time = outstr.mid( start_num, flag - start_num );
          std::cout << time.toStdString() << std::endl;
          qDebug( "Online
    " );
        }
        else
        {
          qDebug( "Offline
    " );
        }
      }
    }
    int main( int argc, char* argv[] )
    {
      QCoreApplication a( argc, argv );
      checkOnline( "192.168.15.90" );
    
      return a.exec();
    }

    代码运行结果如下





  • 相关阅读:
    linux 运维
    mariadb replication
    phpmyadmin
    Objective-C设计模式——单例Singleton(对象创建)
    收藏iOS学习资料
    axios拦截器
    vue单页面优化
    html设置http缓存代码
    js数组去重,排序的几种方法
    前端移动端问题
  • 原文地址:https://www.cnblogs.com/zx-hit/p/12809663.html
Copyright © 2020-2023  润新知