c++ - 无法从 socket_.async_receive_from 接收数据

标签 c++ boost-asio

我在 udp_server 类中创建了异步函数 start_receive() 和 start_send()。但是通过代码不会停止并等待“async_receive_from”行。这是我的代码;

class udp_server
{
public:
  udp_server(boost::shared_ptr< boost::asio::io_service > io_service,
        const std::string& host,
        const std::string& port
    ) : io_service_(*io_service), socket_(*io_service, udp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 7777))
  {
        udp::resolver resolver(io_service_);
        udp::resolver::query query(udp::v4(), host, port);
        udp::resolver::iterator iter = resolver.resolve(query);
        endpoint_ = *iter;
        std::cout << "end point>>" << endpoint_ << std::endl;
        //start_receive();
  }

void start_receive()
  {
  std::cout << "Gotin>>start_receive" << std::endl;
  //std::cout << "Gotin>>" << std::endl;
    socket_.async_receive_from(
          boost::asio::buffer(recv_buffer_), remote_endpoint_,
          boost::bind(&udp_server::handle_receive, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred
          ));
  }

  void handle_receive(const boost::system::error_code& error, std::size_t msg_len)
  {
  using namespace std;
    std::cout << "Got>> handle_receive" << std::endl;
    //size_t data_length = data.size();
    //std::cout.write(recv_buffer_.data(), recv_buffer_.size());
    if (!error || error == boost::asio::error::message_size)
    {
        //std::cout << "msg_len:" << msg_len << std::endl;
        std::cout.write(recv_buffer_.data(), msg_len);
        std::cout << ", " << msg_len << std::endl;
        start_send(recv_buffer_.data(), msg_len);
        recv_buffer_.assign(0);
        start_receive();
    }
  }

  void start_send(const std::string& msg, std::size_t msg_len)
  {
        socket_.async_send_to(boost::asio::buffer(msg, msg_len), remote_endpoint_,
          boost::bind(&udp_server::handle_send, this, msg,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
  }

  void handle_send(const std::string& msg/*message*/,
       const boost::system::error_code& /*error*/,
       std::size_t tx_size/*bytes_transferred*/)
  {
        std::cout << "send>" << msg << std::endl;
        //std::cout << "size>" << tx_size << std::endl;
        //std::cout << "send>" << msg << std::endl;
  }

private:
  boost::asio::io_service& io_service_;
  udp::endpoint endpoint_;
  udp::socket socket_;
  udp::endpoint remote_endpoint_;
  boost::array<char, 1000> recv_buffer_;
};

int main()
{
  try
  {

    boost::shared_ptr< boost::asio::io_service > io_service(new boost::asio::io_service);

    udp_server server(io_service, "localhost", "6666");
    //boost::asio::io_service io_service;
    //udp_server server(io_service);
    //io_service.run();
    boost::thread_group tgroup;
    tgroup.create_thread( boost::bind(&udp_server::start_receive, &server));
    tgroup.join_all();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

在控制台上,我打印了“Gotin>>start_receive”行,但代码突然终止。因为它应该接收一些字节才能在“socket_.async_receive_from(”线上接收到,但它没有。我从来没有在控制台上看到“Got>> handle_receive”。我错过了什么?

最佳答案

您应该将线程提供给 io_service.run(),而不是 socket_.async_receive_from()

你的代码应该是这样的:

tgroup.create_thread([&]{ io_service->run(); }); // or bind(&io_service::run, io_service)
io_service.dispatch([&]{ server.start_receive(); }); // or bind(&udp_server::start_receive, &server)
tgroup.join_all();

附带说明 - 您不会将 io_service 存储到共享指针中。您的程序中没有共享所有权。

关于c++ - 无法从 socket_.async_receive_from 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38786262/

相关文章:

c++ - 构建库时的 Clang 和 undefined symbol

C++ 模板类型特征问题

c++ - 为什么这里 `boost::bind()`不能换成 `std::bind()`呢?

c++ - boost::asio::ip::tcp::socket 中的 async_read_some() 是如何工作的?

c++ - boost 编译问题

sockets - boost::asio 套接字 async_* 链

c++ - UDP - 微突发期间丢失数据

c++ - 如何将数组中的值复制到新数组中?

C++ 代码执行缓慢

c++ - 指向类的静态成员的指针