无法从socket_.async_receive_from接收数据

时间:2016-08-05 09:49:00

标签: 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;
}
在控制台上,我得到了一行&#34; Gotin&gt;&gt; start_receive&#34;打印但代码突然终止。因为它应该接收一些字节以便在线接收&#34; socket_.async_receive_from(&#34;,但它没有。我从来没有看到&#34;得到&gt;&gt; handle_receive&#34;在控制台上。我错过了什么?

1 个答案:

答案 0 :(得分:1)

您应该为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存储到共享指针中。您的计划中没有共享所有权。