在C++中使用pthread时编译错误

发布于 2024-11-04 11:54:10 字数 1855 浏览 0 评论 0原文

我正在用 C++ 编写一个嗅探器类,如下代码。

#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
}

然而,当我编译代码时,g++ 说

../Capture.cpp: In member function ‘bool Capture::startCapture()’:
../Capture.cpp:30: error: argument of type ‘void* (Capture::)(void*)’ does not match ‘void* (*)(void*)’

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

I'm writing a sniffer class with c++ as following code.

#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
}

However, when I compiled the code, g++ said that

../Capture.cpp: In member function ‘bool Capture::startCapture()’:
../Capture.cpp:30: error: argument of type ‘void* (Capture::)(void*)’ does not match ‘void* (*)(void*)’

Could somebody help me out of this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-11-11 11:54:10

C++ 类中的成员函数将 this 作为隐藏参数传递,C 函数 pthread_create 无法理解 this 是什么,因为静态成员函数不传递 < code>this 你可以使用这样的静态函数:

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



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

A member function in C++ class passes this as hidden parameter, the C function pthread_create cannot understand what this is, Since static member functions dont pass this you can use a static function like this:

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



pthread_create(..., Capture::sniffer_thread, NULL);
紫竹語嫣☆ 2024-11-11 11:54:10

首先:Capture::sniffer_thread 是静态的吗?因为成员函数指针不是函数指针!这两种指针是不兼容的!

First of all: is Capture::sniffer_thread static? cause a member function pointer is not a function pointer! These two kinds of pointers are incompatible!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文