简体   繁体   中英

Boost asio, async_read and acync_write not calling callbacks

I'm encapsulating the boost-asio socket, but I got an issue with it, but neither async_read nor async_write calls their callback function and I don't understand why.

I've tried using async_read_some but had the same issue.

Here's the code I've written so far

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

The malloc bit is temporary until I find a way to do it without using C.

I'd be really thankful if someone could enlighten me on that issue

You have to call io_service::reset before second and later calls to io_service::run . And you probably want to use synchronous API instead, as your current approach absolutely defeats the purpose of asynchronicity.

I'm with yuri: prefer non-async unless you know what you're doing.

It could look like this: 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();
}

If you want async behaviour, you have to manage the lifetimes of each buffer/connection-specific resource. There are many examples of that, eg in the docs or here: http://coliru.stacked-crooked.com/a/95e2000e49b4db1d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM