为什么调用 pcap_loop 函数后,我必须等待几秒钟才能捕获第一个数据包?
void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
int main(int argc, char * argv[])
{
pcap_t * handle;
char dev_name[] = "en0";
char err_buf[PCAP_ERRBUF_SIZE];
handle = pcap_open_live(dev_name, 4096, 1, 0, err_buf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n" , dev_name , err_buf);
exit(1);
}
pcap_loop(handle, 1, print_packet, NULL);
pcap_close(handle);
exit(0);
}
void print_packet(u_char * args, const struct pcap_pkthdr * hdr, const u_char * buff)
{
/* ... */
}