test2抓包 PCAP

来源:互联网 发布:C语言荷兰国旗问题 编辑:程序博客网 时间:2024/06/09 17:06
#include <pcap.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>


int main()
{
  char errBuf[PCAP_ERRBUF_SIZE], * devStr;


  /* get a device */
  devStr = pcap_lookupdev(errBuf);


  if(devStr)
  {
    printf("success: device: %s\n", devStr);
  }
  else
  {
    printf("error: %s\n", errBuf);
    exit(1);
  }


  /* open a device, wait until a packet arrives */
  pcap_t * device = pcap_open_live(devStr, 65535, 1, 0, errBuf);


  if(!device)
  {
    printf("error: pcap_open_live(): %s\n", errBuf);
    exit(1);
  }


  /* wait a packet to arrive */
  struct pcap_pkthdr packet;
  const u_char * pktStr = pcap_next(device, &packet);


  if(!pktStr)
  {
    printf("did not capture a packet!\n");
    exit(1);
  }


  printf("Packet length: %d\n", packet.len);
  printf("Number of bytes: %d\n", packet.caplen);
  printf("Recieved time: %s\n", ctime((const time_t *)&packet.ts.tv_sec)); 


  pcap_close(device);


  return 0;
}






[root@localhost 1]# ./test5
success: device: eth0
Packet length: 92
Number of bytes: 92
Recieved time: Mon Oct 10 01:26:36 2016
0 0
原创粉丝点击