求有流量时才计时的网络计时器
发布网友
发布时间:2022-05-29 05:43
我来回答
共1个回答
热心网友
时间:2023-10-08 19:31
下面是基于ace定时器写的网络流量计算代码。你只需要把里面的时间间隔设为你需要的,比如1秒。然后定义一个时间变量,只在每次有流量的时候进行累加,就是网络速度不为0的时候。不过这个是算电脑上网卡流量的~还需要ace的库才可以跑~代码如下:
用这个要先配置好ace,其实不用ace的话直接在个while里面sleep几秒再用handle_timeout也是可以的
#include "ace/Reactor.h"
#include <windows.h>
#include "Iphlpapi.h"
#include "iprtrmib.h"
#include <iostream> //清屏
#include "ace/Event_Handler.h"
#include "ace/Timer_Heap_T.h"
#include "ace/Timer_Queue_Adapters.h"
typedef ACE_Event_Handler_Handle_Timeout_Upcall<ACE_Null_Mutex> Upcall;
typedef ACE_Timer_Heap_T<ACE_Event_Handler *,Upcall,ACE_Null_Mutex> Timer_Heap;
//typedef ACE_Timer_Heap_Iterator_T<ACE_Event_Handler *,Upcall,ACE_Null_Mutex> Timer_Heap_Iterator;
typedef ACE_Thread_Timer_Queue_Adapter<Timer_Heap> Thread_Timer_Queue;
//DWORD index;
class CTimerHandler : public ACE_Event_Handler
{
public:
DWORD dwLastOut;
DWORD dwLastIn;
DWORD dwLastTime;
virtual int handle_timeout(const ACE_Time_Value &tv, const void *arg = 0)
{
//SYSTEMTIME time;
//GetLocalTime(&time);
//printf("%d:%d:%d\ninput 0 to end\n", time.wHour, time.wMinute, time.wSecond);
//Sleep(3000);printf("qwe\n");
MIB_IFROW mi;
mi.dwIndex = 2;//2
GetIfEntry(&mi);
DWORD thisUp = mi.dwOutOctets;
system("cls");//清屏
DWORD dwthisTime = GetTickCount();
DWORD dwPastTime = dwthisTime - dwLastTime;
dwLastTime = dwthisTime;
dwPastTime /= 1000;//毫秒转换为秒
dwPastTime = (dwPastTime > 1) ? dwPastTime : 1; //不足1秒取1秒
if (mi.dwInOctets - dwLastIn > 1024)
{
printf("发送: %dK/s 接收%dK/s\n", (mi.dwOutOctets - dwLastOut)/ 1024 / dwPastTime, (mi.dwInOctets - dwLastIn)/ 1024 / dwPastTime);
}
else
printf("发送: %dB/s 接收%dB/s\n", (mi.dwOutOctets - dwLastOut) / dwPastTime, (mi.dwInOctets - dwLastIn) / dwPastTime);
printf("带宽%db/s\n", mi.dwSpeed);
dwLastOut = mi.dwOutOctets;
dwLastIn = mi.dwInOctets;
printf("发送: %dBytes 接收%dBytes\n", dwLastOut, dwLastIn);
return 0;
}
};
int ACE_TMAIN(int, ACE_TCHAR *[])
{
char tempChar;
ULONG uListSize=1;
PIP_ADAPTER_INFO pAdapter; // 定义PIP_ADAPTER_INFO结构存储网卡信息
int nAdapterIndex = 0;
DWORD dwRet = GetAdaptersInfo((PIP_ADAPTER_INFO)&tempChar, &uListSize); // 关键函数
if (dwRet == ERROR_BUFFER_OVERFLOW)
{
PIP_ADAPTER_INFO pAdapterListBuffer = (PIP_ADAPTER_INFO)new(char[uListSize]);
DWORD dwRet = GetAdaptersInfo(pAdapterListBuffer, &uListSize);
if (dwRet == ERROR_SUCCESS)
{
pAdapter = pAdapterListBuffer;
while (pAdapter) // 枚举网卡
{
/*string strTemp;*/
printf("网卡的名字:%s\n",pAdapter->AdapterName);// 网卡名字
printf("网卡的index:%d\n",pAdapter->Index);// 编号
// strTemp = CString("\\Device\\NPF_") + strTemp;??? // 加上前缀
// strcpy(AdapterList[nAdapterIndex].szDeviceName,strTemp);
// strcpy(AdapterList[nAdapterIndex].szIPAddrStr, pAdapter->IpAddressList.IpAddress.String );// IP
// FormatMACToStr( AdapterList[nAdapterIndex].szHWAddrStr,pAdapter->Address ); // MAC
// AdapterList[nAdapterIndex].filled = true;
pAdapter = pAdapter->Next;
nAdapterIndex ++;
}
delete pAdapterListBuffer;
}
}
//ULONG ListSize = (ULONG)sizeof(PIP_ADAPTER_INFO);
//PIP_ADAPTER_INFO *pAdapterListBuffer = new PIP_ADAPTER_INFO;
//GetAdaptersInfo(pAdapterListBuffer, &uListSize);
//printf("%d",pAdapterListBuffer->Index);
//delete pAdapterListBuffer;
CTimerHandler * pTH = new CTimerHandler();
//ACE_Reactor reactor;
//int id = ACE_Reactor::instance()->schele_timer(pTH, NULL, ACE_Time_Value(0), ACE_Time_Value(5));
////ACE_Reactor::instance()->run_reactor_event_loop();
//while (1)
//{
// ACE_Reactor::instance()->handle_events();
// printf("....\n");
//}
//LPWSTR name = L"";
//GetAdapterIndex(
// name,
// &index
// );
Thread_Timer_Queue timer;
timer.schele(pTH, NULL, ACE_Time_Value(1) + ACE_OS::gettimeofday(), ACE_Time_Value(1));
timer.activate();
int stop;
printf("input 0 to end\n");
scanf_s("%d",&stop);
if (0 == stop)
{
//ACE_Reactor::instance()->cancel_Timer(id);
timer.deactivate();
timer.wait();
}
//int i = -7;
//i = 0xEFFFFFFF & i;
////i = i>>1;
//printf("%o\n",i);
scanf_s("%d",&stop);
return 0;
}