1. 程式人生 > >C++在windows下獲取本地流量和網速的程式

C++在windows下獲取本地流量和網速的程式

#include <iostream> 
#include <fstream> 
#include <string>
#include <cstdlib>
#include <windows.h>
using namespace std; 
#define time 2;//採集週期(S)
int main() 

unsigned  long number2[2] = {0};//傳送流量
unsigned  long number1[2] = {0};//接受流量
float arv = 0 ;//平均速度
for(int count = 0;count<2;count++){
system("netstat -es >d:\\4.txt");
char s[1024] = {" "}; 
char data1[20]={" "};
char data2[20]={" "};
int k = 0;
char* path = "d:\\4.txt"; 
fstream fh(path); 
int i = 0;
while(fh.getline(s,1024)) 

if(i==4){
cout << s << endl;
break;
}
i++;


fh.close(); 
int signal = 0;
for(int j = 10;j<1024;j++){

if(s[j]!=32&&signal ==0){
for(k=0;s[j]!=32;k++){
data1[k] = s[j];
cout<<s[j];

j++;


}
cout<<"data1 over"<<endl;
signal = 1;
}
else if(s[j]!=32&&signal == 1){
for(k = 0;s[j]!=0;k++){
data2[k] = s[j];
cout<<s[j];

j++;
}
cout<<"data2 over"<<endl;
signal = 2;
}
else if(s[j]==32&&signal == 2){

break;
}

}

for(int t = 0;t<20;t++){
cout<<data1[t];
}

string str(data1);
cout<<endl<<"data1:"<<str<<endl;
string str2(data2);
cout<<endl<<"data2:"<<str2<<endl;
number1[count] = atoi(str.c_str());

cout<<"number1:"<<number1[count]<<endl;
number2[count] = atoi(str2.c_str());
cout<<"number2:"<<number2[count]<<endl;

if(count == 0){
int sleept = time;
int sleeptime = sleept *1000;
Sleep(sleeptime);
}
}
arv = (number1[1]+number2[1]-number1[0]-number2[0])/time;//得到平均速度
cout<<"arv="<<arv;
    return 0;