提高:: ASIO :: async_write,写数据大于65536字节 [英] boost::asio::async_write, writing data larger than 65536 bytes

查看:1765
本文介绍了提高:: ASIO :: async_write,写数据大于65536字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一个socket写入JPEG图像帧到客户端使用 async_write()。我用升压异步TCP daytime服务器的例子作为一个起点。

I'm attempting to write jpeg frames via a socket to a client using async_write(). I used the boost asynchronous TCP daytime server example as a starting point.

#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 :: async_write()与同步调用的boost :: ASIO ::写()正确的字节量被传输到客户端。

When reading a large file from the server using a client, the server will only write a maximum of 65536 bytes. If I replace the boost::asio::async_write() call with a synchronous call boost::asio::write() the correct amount of bytes are transferred to the client.

所以我想我的问题是,我怎么能发送使用超过65536字节的boost :: ASIO :: async_write()?任何帮助将大大AP preciated。

So I suppose my question is, how can I send more than 65536 bytes using boost::asio::async_write()? Any help would be greatly appreciated.

推荐答案

一个问题是使用 async_write 函数的数据将不会立即被功能,但在有些发在启动方法完成,本地消息_ 变量将被销毁,升压后的时间:: ASIO ::缓​​冲不复制消息_ 的内容。它存储只是一个参考吧。其结果是未predictable。可能是65536个字节的传输是此行为的结果。

An issue is that using the async_write function data will be send not immediately by the function but in some time after the start method is finished and the local message_ variable will be destroyed and the boost::asio::buffer does not copy the content of message_. It stores only a reference to it. The result is unpredictable. May be transmission of 65536 bytes is the result of this behavior.

这篇关于提高:: ASIO :: async_write,写数据大于65536字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆