提升asio,async_read和acync_write不调用回调

时间:2017-10-02 19:19:15

标签: c++ boost-asio

我正在封装boost-asio套接字,但我遇到了问题,但async_readasync_write都没有调用它们的回调函数,我不明白为什么。

我尝试使用async_read_some,但遇到了同样的问题。

这是我到目前为止编写的代码

#include <iostream>
#include "socket.hpp"

Socket::Socket()
{
  boost::asio::ip::tcp::endpoint    ep_tmp(boost::asio::ip::tcp::v4(), 4242);

  endpoint = ep_tmp;
  acceptor = new boost::asio::ip::tcp::acceptor(ios, endpoint);
  tcp_socket = new boost::asio::ip::tcp::socket(ios);
  acceptor->listen();
}

Socket::~Socket()
{
  delete(acceptor);
  delete(tcp_socket);
}

void        Socket::get_connection()
{
  acceptor->async_accept(*tcp_socket, [](const boost::system::error_code &ec)
             {
               std::cout << "Connection received." << std::endl;
               if (ec)
                 std::cout << "Error " << ec << std::endl;
             });
  this->exec();
}

void        Socket::send(std::string &message)
{
  async_write(*tcp_socket, boost::asio::buffer(message),
          [](const boost::system::error_code &ec,
         std::size_t bytes_transferred)
          {
        std::cout << "Sending datas." << std::endl;
        if (ec)
          std::cout << "Error " << ec << std::endl;
        else
          std::cout << bytes_transferred << " bytes transferred." << std::endl;
          });
}

void        Socket::receive(void)
{
  char      *buf;

  buf = (char *)malloc(sizeof(char) * 50);
  buf = (char *)memset(buf, 0, 50);
  async_read(*tcp_socket, boost::asio::buffer(buf, 50),
         [](const boost::system::error_code &ec,
        std::size_t bytes_transferred)
         {
           std::cout << "Receiving datas." << std::endl;
           if (ec)
         std::cout << "Error " << ec << std::endl;
           else
         std::cout << bytes_transferred
               << " bytes transferred." << std::endl;
         });
}

void        Socket::exec(void)
{
  ios.run();
}

int     main()
{
  Socket    serv;
  std::string   data_test;

  data_test = "Test\n";
  serv.get_connection();
  serv.send(data_test);
  serv.exec();
  serv.receive();
  serv.exec();
  return (0);
}

malloc位是临时的,直到我找到了一种不使用C的方法。

如果有人能在这个问题上给我启发,我真的很感激

2 个答案:

答案 0 :(得分:1)

您必须在第二次及以后拨打io_service::run之前致电io_service::reset。你可能想要使用同步API,因为你当前的方法绝对违背了异步性的目的。

答案 1 :(得分:0)

我和yuri:除非你知道自己在做什么,否则更喜欢非异步。

它可能如下所示:http://coliru.stacked-crooked.com/a/523a7828a9aee4b2

#include <boost/asio.hpp>
#include <iostream>

namespace ba = boost::asio;
using ba::ip::tcp;

class Socket {
  public:
    Socket() { acceptor.listen(); }

    void get_connection();
    void exec();
    void send(std::string const &message);
    void receive(void);

  private:
    ba::io_service ios;
    tcp::endpoint endpoint{ tcp::v4(), 4242 };
    tcp::acceptor acceptor{ ios, endpoint };
    tcp::socket tcp_socket{ ios };
};

void Socket::get_connection() {
    acceptor.accept(tcp_socket);
    std::cout << "Connection received.\n";
}

void Socket::send(std::string const &message) {
    std::cout << "Sending datas.\n";
    auto bytes_transferred = ba::write(tcp_socket, ba::buffer(message));
    std::cout << bytes_transferred << " bytes transferred.\n";
}

void Socket::receive(void) {
    std::cout << "Receiving datas.\n";

    char buf[50] = { 0 };
    auto bytes_transferred = ba::read(tcp_socket, ba::buffer(buf));
    std::cout << bytes_transferred << " bytes transferred.\n";
}

int main() {
    Socket serv;

    serv.get_connection();
    serv.send("Test\n");
    serv.receive();
}

如果您想要异步行为,则必须管理每个缓冲区/特定于连接的资源的生命周期。有许多例子,例如在文档中或此处:http://coliru.stacked-crooked.com/a/95e2000e49b4db1d