189 8069 5689

Qt/C++获取当前网络网速-创新互联

#include#include#includeclass NetInfo : public QThread
{Q_OBJECT
public:
    explicit NetInfo(QObject* parent = nullptr);

protected:
    void run();

signals:
    void sendCurrentNetInfo(const QString netInfo);
};
NetInfo::NetInfo(QObject* parent)
    : QThread(parent)
{}

void NetInfo::run()
{PMIB_IFTABLE pTable = nullptr;
    DWORD dword = 0;
    ULONG retCode = GetIfTable(pTable, &dword, true);
    if(retCode == ERROR_NOT_SUPPORTED)
        return;
    if(retCode == ERROR_INSUFFICIENT_BUFFER)
        pTable = (PMIB_IFTABLE)new BYTE[65535];
	//上传速度
    DWORD dwIn = 0;
    //下载速度
    DWORD dwOut = 0;
    //上传最后字节
    DWORD dwLastIn = 0;
    //下载最后字节
    DWORD dwLastOut = 0;

    while(true)
    {GetIfTable(pTable, &dword, true);
        DWORD dwInOc = 0;
        DWORD dwOutOc = 0;
        for(UINT i = 0; i< pTable->dwNumEntries; i++)
        {MIB_IFROW row = pTable->table[i];
            dwInOc += row.dwInOctets;
            dwOutOc += row.dwOutOctets;
        }
        dwIn = dwInOc - dwLastIn;
        dwOut = dwOutOc - dwLastOut;

        if(dwLastIn<= 0)
            dwIn = 0;
        else
            dwIn = dwIn / 1024;

        if(dwLastOut<= 0)
            dwOut = 0;
        else
            dwOut = dwOut / 1024;

        dwLastIn = dwInOc;
        dwLastOut = dwOutOc;

        emit sendCurrentNetInfo(QString("%1kb/s").arg(dwIn));
        QThread::msleep(1000);
    }

    delete [] pTable;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

站在用户的角度思考问题,与客户深入沟通,找到巴彦淖尔网站设计与巴彦淖尔网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、成都网站设计、企业官网、英文网站、手机端网站、网站推广、域名注册雅安服务器托管、企业邮箱。业务覆盖巴彦淖尔地区。
新闻名称:Qt/C++获取当前网络网速-创新互联
浏览路径:http://cdxtjz.cn/article/ghgpg.html

其他资讯