接受Boost ASIO异步的处理程序

时间:2015-10-22 23:32:09

标签: c++ boost boost-asio

所以我试图用Boost ASIO编写一个类来发送数据异步。通过TCP套接字。

我的cpp文件

#include <TcpServer.h>
using namespace boost::asio;
using namespace boost::asio::ip;

TcpServer::TcpServer(unsigned short port = 1700)
: ipPort(port){
io_service svc;  
tcp::socket socket(svc);
tcp::acceptor acc(svc, tcp::endpoint(tcp::v4(), ipPort));
acc.listen();
acc.async_accept(socket, TcpServer::Accept_Handler);
 boost::thread bt(boost::bind(&io_service::run, &svc));
}

void TcpServer::Write_Handler(const boost::system::error_code& ec){
  if (!ec)
    {
     std::cout << "Just sent some %0.2f" << yawData << std::endl;
    }
}

 void TcpServer::Accept_Handler(const boost::system::error_code& ec){
   if (!ec)
     {
      async_write(socket, buffer("data"), TcpServer::Write_Handler);
    }
}

void UpdateYaw(double data) {
   yawData = data;

}

  TcpServer::~TcpServer(){
    svc.stop();
 }

及其标题

#pragma once
#include <boost/asio.hpp>
#include <boost/thread.hpp>


 class TcpServer {
  private:
   double yawData = 0;
   unsigned short ipPort;
   void Write_Handler(const boost::system::error_code&);
   void Accept_Handler(const boost::system::error_code&);
  public:
   TcpServer(unsigned short );
   void UpdateYaw(double);
   ~TcpServer();
  };

我在编译时遇到的错误是

 no matching function for call to ‘boost::asio::basic_socket_acceptor<boost::asio::ip::tcp>::async_accept(boost::asio::ip::tcp::socket&, <unresolved overloaded function type>)’acc.async_accept(socket, TcpServer::Accept_Handler);

它根本不喜欢我作为参数传递给async_accept的处理程序。当然,这发生在另一个处理程序(Write_handler)以及async_write。 知道我可能做错了吗?

1 个答案:

答案 0 :(得分:0)

std :: bind(&amp; TcpServer :: Accept_Handler,this,std :: placeholder :: _ 1)。并且双重检查写入处理程序签名