带宽speed

#-------------------------------------------------
#
# Project created by QtCreator 2018-10-16T21:52:56
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = 12121
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

LIBS+=-liphlpapi

SOURCES += main.cpp
//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#include <winsock2.h>
#include <ws2tcpip.h>
//#pragma comment(lib, "IPHLPAPI.lib")
//#pragma comment(lib,"iphlpapi.lib")

#include "windows.h"
#include "winsock.h"
#include "iphlpapi.h"
#include <string.h>
#include <stdio.h>
#include <QTextCodec>

int main() {

    string NetSpeedInfo;
    PMIB_IFTABLE    m_pTable = NULL;
    DWORD    m_dwAdapters = 0;
    ULONG    uRetCode = GetIfTable(m_pTable, &m_dwAdapters, TRUE);
    if (uRetCode == ERROR_NOT_SUPPORTED)
    {
        return -1;
    }

    if (uRetCode == ERROR_INSUFFICIENT_BUFFER)
    {
        m_pTable = (PMIB_IFTABLE)new BYTE[65535];   //假设端口数不超过65535个
    }
    QTextCodec *codec=QTextCodec::codecForName("utf-8");
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);


    DWORD   dwLastIn = 0;           //上一秒钟的接收字节数
    DWORD   dwLastOut = 0;          //上一秒钟的发送字节数
    DWORD   dwBandIn = 0;           //下载速度
    DWORD   dwBandOut = 0;          //上传速度

    while (true)
    {
        NetSpeedInfo.clear();
        GetIfTable(m_pTable, &m_dwAdapters, TRUE);
        DWORD   dwInOctets = 0;
        DWORD   dwOutOctets = 0;

        //将所有端口的流量进行统计
        for (UINT i = 0; i < m_pTable->dwNumEntries; i++)
        {
            MIB_IFROW   Row = m_pTable->table[i];
            Row.dwIndex = m_pTable->table[i].dwIndex;
            if (GetIfEntry(&Row) == NO_ERROR)
            {
                //在这里可以判断IfRow中的dwType,MIB_IF_TYPE_PPP是拨号
                //ifRow.dwSpeed就是速率
                std::cout << Row.dwType << std::endl;
                std::cout << "速率:" << Row.dwSpeed << std::endl;
            }

            dwInOctets += Row.dwInOctets;
            dwOutOctets += Row.dwOutOctets;
        }

        dwBandIn = dwInOctets - dwLastIn;       //下载速度
        dwBandOut = dwOutOctets - dwLastOut;    //上传速速
        if (dwLastIn <= 0)
        {
            dwBandIn = 0;
        }
        else
        {
            dwBandIn = dwBandIn; /// 1024; //b转换成kb
        }

        if (dwLastOut <= 0)
        {
            dwBandOut = 0;
        }
        else
        {
            dwBandOut = dwBandOut; /// 1024;   //b转换成kb
        }

        dwLastIn = dwInOctets;
        dwLastOut = dwOutOctets;

        std::cout <<
                     "收到字节:" << dwLastIn << " bytes\r\n" <<
                     "发送字节:" << dwLastOut << " bytes\r\n" <<
                     "下行速度:" << dwBandIn << " b\r\n" <<
                     "上行速度:" << dwBandOut << "b\r\n" << std::endl;



        std::cout << "带宽使用率:" << (dwBandIn + dwBandOut) / 8. / (150.*1024. * 1024 / 8.) * 100 << "%" << std::endl;
        printf("--------------------------\n");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40569991/article/details/83098771