你要是看过basic_stream_socket的文档,里面提到async_write_some不能保证将所有要发送的数据都发出去。并且提到如果想这样做,需要使用boost asio的async_write

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/basic_stream_socket/async_write_some.html

  1. Remarks
  2. The write operation may not transmit all of the data to the peer. Consider using the async_write function if you need to ensure that all data is written before the asynchronous operation completes.

但是这几天我就遇到一个问题,以前一直都是一次发送成功的。

我想发送54个字节的数据,可是每次都是只发9个字节。因此只好自己写了一个重试发送的递归函数。也很简单,通过bind,每次传递想要发送的字节数木和发送开始位置给异步回调函数。

代码参考如下:

  1. void Sign::AfterWriteMessage(error_code const& ec, size_t bytes_transferred, size_t expected_size,  size_t offset) {
  2. if (ec) {
  3. BOOSTER_ERROR("AfterWriteMessage") << "write message failed, error code:" << ec.value()
  4. << " category name:" << ec.category().name()
  5. << " id_:" << id_
  6. << " address:" << address
  7. << " message:" << ec.message();
  8. Close();
  9. return;
  10. }
  11. BOOSTER_DEBUG("AfterWriteMessage") << "thread id: " << this_thread::get_id() << " send_buffer: " << PrintBytesAsHexString(send_buffer, bytes_transferred) << " sent size:" << bytes_transferred;
  12. BOOSTER_DEBUG("AfterWriteMessage") << "thread id: " << this_thread::get_id() << " send_buffer: " << PrintBytesAsHexString(send_buffer, expected_size) << " expected size:" << expected_size;
  13. size_t resend_size = expected_size - bytes_transferred;
  14. if (resend_size > 0) {
  15. size_t new_offset = offset + bytes_transferred;
  16. async_write(socket, buffer((void*)&send_buffer[new_offset], resend_size),
  17. strand_.wrap(bind(&Sign::AfterWriteMessage, shared_from_this(), _1, _2, resend_size, new_offset)));
  18. return;
  19. }
  20. // do your business after send succeeds
  21. }
  22. void Sign::SendMessage(size_t size) {
  23. //  BOOSTER_DEBUG("SendMessage") << "thread id: " << this_thread::get_id() << " send_buffer: " << PrintBytesAsHexString(send_buffer, size) << " size:" << size;
  24. async_write(socket, buffer(send_buffer, size),
  25. strand_.wrap(bind(&Sign::AfterWriteMessage, shared_from_this(), _1, _2, size, 0)));
  26. }

但是为什么呢?难道真的是bug. 请看下一篇。

boost::asio async_write也不能保证一次发完所有数据 一的更多相关文章

  1. boost::asio async_write也不能保证一次发完所有数据 二

    只有看boost源码才能弄明白发生了什么.首先我是将vector里面写入了数据,然后用boost::asio::buffer将vector构造成了mutable_buffer_1对象. 参考该文档的重 ...

  2. boost::asio译文

        Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...

  3. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

  4. Boost.Asio技术文档

    Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...

  5. Boost.Asio的使用技巧

    基本概念 Asio proactor I/O服务 work类 run() vs poll() stop() post() vs dispatch() buffer类 缓冲区管理 I/O对象 socke ...

  6. boost asio 学习(九) boost::asio 网络封装

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...

  7. 浅谈 Boost.Asio 的多线程模型

    Boost.Asio 有两种支持多线程的方式,第一种方式比较简单:在多线程的场景下,每个线程都持有一个io_service,并且每个线程都调用各自的io_service的run()方法. 另一种支持多 ...

  8. <转>浅谈 Boost.Asio 的多线程模型

    本文转自:http://senlinzhan.github.io/2017/09/17/boost-asio/ Boost.Asio 有两种支持多线程的方式,第一种方式比较简单:在多线程的场景下,每个 ...

  9. BOOST ASIO 学习专贴

    本文已于20170903更新完毕,所有boost asio 代码均为本人手抄.编译器为vs2013,并且所有代码已经上传,本文下方可下载源码 为了学习boost asio库,我是从boost的官方bo ...

随机推荐

  1. Unity Notes调制粒子系统的颗粒的最大数目

    Unity该粒子系统是很容易使用.这样的问题是在实际的过程中遇到的:以控制的粒子系统组件的动态需要可产生颗粒的最大数目. 看doc他说,有maxParticles控制.却没有这个开放的參数.仅仅能通过 ...

  2. CentOS 64位上编译 Hadoop 2.6.0

    Hadoop不提供64位编译好的版本号,仅仅能用源代码自行编译64位版本号. 学习一项技术从安装開始.学习hadoop要从编译開始. 1.操作系统编译环境 yum install cmake lzo- ...

  3. js php xmlrequest 上传图片

    本来想用插件上传图片的,后来自己写了一个简单的js实现异步的图片上传,不多说上代码很easy upload.php <?php if(isset($_FILES["myfile&quo ...

  4. for循环语句之棋盘放粮食、百鸡百钱、纸张的折叠问题

    1.棋盘放粮食 ; ; i < ; i++) { ; ; j <= i; j++) { x = x * ; } lszl = lszl + x; } double zl = lszl * ...

  5. shell的wc命令统计 head tail命令详解

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  6. 安卓Launcher之获取手机安装的应用列表,安卓launcher

    Launcher中最主要的就是获取所有应用列表的入口以及图标,一般获取的方法有两种: PackageInfo ResolveInfo 运行获取所有APP的Launcher并且允许进行点击事件,进入到应 ...

  7. Android 开发中eclipse 下 DDMS 视图中 sdcard 中文件导入的处理

    首先需要说明下,这里说的sdcard的权限并不是指在Android application程序中设置sdcard的权限读 取问题.而是指在DDMS看到的目录下的那个sdcard目录的权限问题.     ...

  8. java计算两个日期相差多少天

    java计算两个日期相差多少天 public class DateUtil{ public static int betweenDays(Date startDate, Date endDate ) ...

  9. 设计模式(四)原型模式Prototype(创建型)

      设计模式(四)原型模式Prototype(创建型) 1.   概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...

  10. php 遍历文件夹及文件,获取文件名和文件路径存入数据库中

    <?php header("Content-Type:text/html; charset=gbk"); require('../../include/connect.php ...