我有一个允许用户扫描网络的 GUI 程序,问题是当调用 pcap_loop 函数时,我的 GUI 程序变得无响应。(pcap_loop 阻塞了当前线程)。
当我尝试使用 pthreads 时,我在 pcap_loop 函数处遇到了 SIGSEGV 错误。为什么?就好像线程看不到 procPacket 函数本身一样。
void procPacket(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
//show packets here
}
void* pcapLooper(void* param)
{
pcap_t* handler = (pcap_t*) param;
pcap_loop(handler, 900 ,procPacket, NULL );
}
//some function that runs when a button is pressed
//handler has been opened through pcap_open_live
pthread_t scanner;
int t = pthread_create(&scanner,NULL,&pcapLooper, &handler );
if(t)
{
std::cout << "failed" << std::endl;
}
pthread_join(scanner,NULL);
//do other stuff.