I have a packet sniffer (see below). To measure bandwidth I think I need to start a timer at the beginning of receving data. Record the number of bytes that has been transmitted and then calculate the average bandwidth. To measure time time to receive/send data I did:
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 turns out to always be 0.0000, which is probably wrong. Is my understanding correct, if yes, Is there any problems with code?
I also tries using milliseconds:
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;
The same output...