【问题标题】:boost::asio::async_write, writing data larger than 65536 bytesboost::asio::async_write,写入大于 65536 字节的数据
【发布时间】:2012-02-24 14:50:47
【问题描述】:

我正在尝试使用async_write() 通过套接字将 jpeg 帧写入客户端。我使用 boost asynchronous TCP daytime server 示例作为起点。

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}

class tcp_connection
  : public boost::enable_shared_from_this<tcp_connection>
{
public:
  typedef boost::shared_ptr<tcp_connection> pointer;

  static pointer create(boost::asio::io_service& io_service)
  {
    return pointer(new tcp_connection(io_service));
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
    message_ = make_daytime_string();

    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

private:
  tcp_connection(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  void handle_write(const boost::system::error_code& /*error*/,
      size_t /*bytes_transferred*/)
  {
  }

  tcp::socket socket_;
  std::string message_;
};

class tcp_server
{
public:
  tcp_server(boost::asio::io_service& io_service)
    : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
  {
    start_accept();
  }

private:
  void start_accept()
  {
    tcp_connection::pointer new_connection =
      tcp_connection::create(acceptor_.io_service());

    acceptor_.async_accept(new_connection->socket(),
        boost::bind(&tcp_server::handle_accept, this, new_connection,
          boost::asio::placeholders::error));
  }

  void handle_accept(tcp_connection::pointer new_connection,
      const boost::system::error_code& error)
  {
    if (!error)
    {
      new_connection->start();
      start_accept();
    }
  }

  tcp::acceptor acceptor_;
};

int main()
{
  try
  {
    boost::asio::io_service io_service;
    tcp_server server(io_service);
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

我修改了执行async_write()的方法如下:

 void start()
  {
    // fileToVector method reads contents of file to vector;
    std::vector<unsigned char> message_ = fileToVector("/tmp/test");

    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

使用客户端从服务器读取大文件时,服务器最多只能写入 65536 字节。如果我用同步调用 boost::asio::write() 替换 boost::asio::async_write() 调用,则正确的字节数会传输到客户端。

所以我想我的问题是,如何使用boost::asio::async_write() 发送超过 65536 个字节?任何帮助将不胜感激。

【问题讨论】:

    标签: boost boost-asio


    【解决方案1】:

    一个问题是,使用async_write 函数时,函数不会立即发送数据,而是在start 方法完成并且本地message_ 变量将被销毁并且boost::asio::buffer 会销毁之后的一段时间内发送数据不要复制message_的内容。它只存储对它的引用。结果是不可预测的。可能是65536字节的传输是这种行为的结果。

    【讨论】:

    • 还请注意transmit_file ASIO 的示例link。使用TransmitFile Windows 函数或 Linux 中的sendfile 可以避免缓冲区分配开销。
    • 问题和你描述的完全一样,缓冲区在 async_write 完成之前就被破坏了。非常感谢您的回复和回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多