客户端使用UDT + boost进行大约200次迭代后发送0字节数据

时间:2016-01-12 16:33:26

标签: c++ udp boost-asio

我尝试使用UDT boost UDT,从其示例,服务器和客户端开始。

当服务器运行无限async_write时,客户端运行无限async_read,但服务器在大约200+循环后停止打印received xx bytes,同时客户端开始打印{ {1}},为什么客户端发送0字节?

这是我的代码

服务器:

sent 0 bytes

客户端:

#include <boost/asio/io_service.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read.hpp>

#include <boost/system/error_code.hpp>
#include <boost/thread.hpp>

#include "udt/connected_protocol/logger/file_log.h"
#include "udt/ip/udt.h"

int main() {

  using udt_protocol = ip::udt<>;
  using Buffer = std::array<uint8_t, 150000>;
  using SocketPtr = std::shared_ptr<udt_protocol::socket>;
  using ReceiveHandler = std::function<void(const boost::system::error_code&, std::size_t,
                             SocketPtr)>;
  using AcceptHandler = std::function<void(const boost::system::error_code&, SocketPtr)>
     ;

  boost::asio::io_service io_service;
  boost::system::error_code resolve_ec;

  Buffer r_buffer2;

  SocketPtr p_socket(std::make_shared<udt_protocol::socket>(io_service));
  udt_protocol::acceptor acceptor(io_service);
  udt_protocol::resolver resolver(io_service);

  udt_protocol::resolver::query acceptor_udt_query(boost::asio::ip::udp::v4(),
                                                   "34567");
  auto acceptor_endpoint_it = resolver.resolve(acceptor_udt_query, resolve_ec);

  if (resolve_ec) {
    BOOST_LOG_TRIVIAL(error) << "Wrong argument provided" << std::endl;
    return 1;
  }

  udt_protocol::endpoint acceptor_endpoint(*acceptor_endpoint_it);

  AcceptHandler accepted;
  ReceiveHandler received_handler;

  accepted = [&](const boost::system::error_code& ec, SocketPtr p_socket) {
    if (ec) {
      BOOST_LOG_TRIVIAL(trace) << "Error on accept : " << ec.value() << " "
                               << ec.message();
      return;
    }

    BOOST_LOG_TRIVIAL(trace) << "Accepted";
    boost::asio::async_read(*p_socket, boost::asio::buffer(r_buffer2),
                            boost::bind(received_handler, _1, _2, p_socket));

    SocketPtr p_new_socket(
        std::make_shared<udt_protocol::socket>(io_service));
    acceptor.async_accept(*p_new_socket,
                          boost::bind(accepted, _1, p_new_socket));
  };

  received_handler = [&](const boost::system::error_code& ec,
                         std::size_t length, SocketPtr p_socket) {
    if (ec) {
      BOOST_LOG_TRIVIAL(trace) << "Error on receive ec : " << ec.value() << " "
                               << ec.message();
      return;
    } else {
        static int counter = 0;
        std::cout << "received : " << length << ", counter = " << counter++ << std::endl;
    }

    boost::asio::async_read(*p_socket, boost::asio::buffer(r_buffer2),
                            boost::bind(received_handler, _1, _2, p_socket));
  };

  boost::system::error_code ec;

  acceptor.open();
  acceptor.bind(acceptor_endpoint, ec);
  acceptor.listen(100, ec);

  acceptor.async_accept(*p_socket, boost::bind(accepted, _1, p_socket));

  boost::thread_group threads;
  for (uint16_t i = 1; i <= boost::thread::hardware_concurrency(); ++i) {
    threads.create_thread([&io_service]() { io_service.run(); });
  }
  threads.join_all();
}

cmd中的结果:enter image description here

0 个答案:

没有答案