【发布时间】:2013-11-29 16:04:24
【问题描述】:
我有一个数据包嗅探器(见下文)。为了测量带宽,我想我需要在接收数据开始时启动一个计时器。记录已传输的字节数,然后计算平均带宽。为了测量接收/发送数据的时间,我做了:
int main() {
//usual packet sniffer staff up untill while loop
struct pcap_pkthdr header;
const unsigned char *packet;
char errbuf[PCAP_ERRBUF_SIZE];
char *device;
pcap_t *pcap_handle;
int i;
device = pcap_lookupdev(errbuf);
if (device == NULL) perror("pcap_lookupdev failed");
printf("Sniffing on device %s\n", device);
pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
if (pcap_handle == NULL) perror("pcap_open_live failed");
while (1) {
//starting the timer
double diff = 0.0;
time_t start;
time_t stop;
char buff[128];
time(&start);
//receiving packet
packet = pcap_next(pcap_handle, &header);
//stopping the timer
time(&stop);
//measuring time of receiving data
diff = difftime(stop, start);
process_packet(packet, header.len, diff);
}
}
diff 结果总是 0.0000,这可能是错误的。我的理解是否正确,如果是,代码有问题吗?
我也尝试使用毫秒:
float diff;
clock_t start;
clock_t stop;
char buff[128];
start = clock();
packet = pcap_next(pcap_handle, &header);//just creates a pointer in no time
stop = clock();
diff = (((float)stop - (float)start) / 1000000.0F ) * 1000;
同样的输出...
【问题讨论】:
标签: c time network-programming packet bandwidth