Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+;中使用pthread时出现编译错误+; 我用C++编写了一个嗅探器类。p> #include "Capture.h" #include <sys/socket.h> #include <cstring> #include <linux/if_ether.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <pthread.h> Capture::Capture(int sniff_socket): sniff_socket_(sniff_socket), sniff_threadID_(0) { } bool Capture::endCapture() { if(!sniff_threadID_) return false; cout << "Asking capture thread to stop\n" <<endl; return !pthread_cancel(sniff_threadID_); } bool Capture::startCapture() { if(pthread_create(&sniff_threadID_, NULL, Capture::sniffer_thread, NULL)) { cerr << "Unable to create capture thread"<<endl; return false; } return true; } void *Capture::sniffer_thread(void *) { int MTU = 2048; char buffer[MTU]; ssize_t msg_len; struct iphdr *ip_header; struct ethhdr *eth_header; struct tcphdr *tcp_header; void *packet; pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); while (1) { msg_len = recvfrom(sniff_socket_, buffer, MTU, 0, NULL, NULL); eth_header = (struct ethhdr*)buffer; ip_header = (iphdr*)(buffer + sizeof(ethhdr)); tcp_header = (struct tcphdr*)(buffer + sizeof(struct ethhdr) + sizeof(struct iphdr)); if(msg_len != -1){ packet = malloc(msg_len); memcpy(packet, buffer, msg_len); pthread_testcancel(); //queue add should be here } else { cerr<<"Error capture thread recvfrom"<<endl; pthread_exit(NULL); } } return NULL; } Capture::~Capture() { // TODO Auto-generated destructor stub }_C++_Multithreading_Pthreads - Fatal编程技术网

在C+;中使用pthread时出现编译错误+; 我用C++编写了一个嗅探器类。p> #include "Capture.h" #include <sys/socket.h> #include <cstring> #include <linux/if_ether.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <pthread.h> Capture::Capture(int sniff_socket): sniff_socket_(sniff_socket), sniff_threadID_(0) { } bool Capture::endCapture() { if(!sniff_threadID_) return false; cout << "Asking capture thread to stop\n" <<endl; return !pthread_cancel(sniff_threadID_); } bool Capture::startCapture() { if(pthread_create(&sniff_threadID_, NULL, Capture::sniffer_thread, NULL)) { cerr << "Unable to create capture thread"<<endl; return false; } return true; } void *Capture::sniffer_thread(void *) { int MTU = 2048; char buffer[MTU]; ssize_t msg_len; struct iphdr *ip_header; struct ethhdr *eth_header; struct tcphdr *tcp_header; void *packet; pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); while (1) { msg_len = recvfrom(sniff_socket_, buffer, MTU, 0, NULL, NULL); eth_header = (struct ethhdr*)buffer; ip_header = (iphdr*)(buffer + sizeof(ethhdr)); tcp_header = (struct tcphdr*)(buffer + sizeof(struct ethhdr) + sizeof(struct iphdr)); if(msg_len != -1){ packet = malloc(msg_len); memcpy(packet, buffer, msg_len); pthread_testcancel(); //queue add should be here } else { cerr<<"Error capture thread recvfrom"<<endl; pthread_exit(NULL); } } return NULL; } Capture::~Capture() { // TODO Auto-generated destructor stub }

在C+;中使用pthread时出现编译错误+; 我用C++编写了一个嗅探器类。p> #include "Capture.h" #include <sys/socket.h> #include <cstring> #include <linux/if_ether.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <pthread.h> Capture::Capture(int sniff_socket): sniff_socket_(sniff_socket), sniff_threadID_(0) { } bool Capture::endCapture() { if(!sniff_threadID_) return false; cout << "Asking capture thread to stop\n" <<endl; return !pthread_cancel(sniff_threadID_); } bool Capture::startCapture() { if(pthread_create(&sniff_threadID_, NULL, Capture::sniffer_thread, NULL)) { cerr << "Unable to create capture thread"<<endl; return false; } return true; } void *Capture::sniffer_thread(void *) { int MTU = 2048; char buffer[MTU]; ssize_t msg_len; struct iphdr *ip_header; struct ethhdr *eth_header; struct tcphdr *tcp_header; void *packet; pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); while (1) { msg_len = recvfrom(sniff_socket_, buffer, MTU, 0, NULL, NULL); eth_header = (struct ethhdr*)buffer; ip_header = (iphdr*)(buffer + sizeof(ethhdr)); tcp_header = (struct tcphdr*)(buffer + sizeof(struct ethhdr) + sizeof(struct iphdr)); if(msg_len != -1){ packet = malloc(msg_len); memcpy(packet, buffer, msg_len); pthread_testcancel(); //queue add should be here } else { cerr<<"Error capture thread recvfrom"<<endl; pthread_exit(NULL); } } return NULL; } Capture::~Capture() { // TODO Auto-generated destructor stub },c++,multithreading,pthreads,C++,Multithreading,Pthreads,有人能帮我解决这个问题吗?首先:Capture::sniffer\u线程是静态的吗?原因成员函数指针不是函数指针!这两种指针不兼容 首先:Capture::sniffer\u线程是静态的吗?原因成员函数指针不是函数指针!这两种指针不兼容 > P> C++类中的一个成员函数通过此< /代码>作为隐藏参数,C函数 pthRead创建< /代码>不能理解什么是 < ,因为静态成员函数不通过 < >,您可以使用这样的静态函数: class Capture { public: s

有人能帮我解决这个问题吗?

首先:Capture::sniffer\u线程是静态的吗?原因成员函数指针不是函数指针!这两种指针不兼容

首先:Capture::sniffer\u线程是静态的吗?原因成员函数指针不是函数指针!这两种指针不兼容

> P> C++类中的一个成员函数通过<代码>此< /代码>作为隐藏参数,C函数<代码> pthRead创建< /代码>不能理解什么是<代码> < <代码>,因为静态成员函数不通过<代码> < <代码> >,您可以使用这样的静态函数:

class Capture
{
    public:
        static void *sniffer_thread(void*);
};



pthread_create(..., Capture::sniffer_thread, NULL);

< C++类中的成员函数传递了<代码>此< /代码>作为隐藏参数,C函数<代码> pthRead创建< /代码>不能理解什么是<代码> < <代码>,因为静态成员函数不通过<代码> < <代码> >,您可以使用这样的静态函数:

class Capture
{
    public:
        static void *sniffer_thread(void*);
};



pthread_create(..., Capture::sniffer_thread, NULL);
see的可能副本,以及see的可能副本,以及