你要是看过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. B - 最大报销额

    注意超时问题,一个题可能有很多种方法解决,但是想到解决方法的同时一定要考虑这个方法的复杂度,特别是对于acm的题,有可能出现超时的情况,很浪费时间 正式比赛中就很遗憾,血的教训. 下面贴上超时的代码并 ...

  2. Android:OptionMenu

    MainActivity: package com.example.optionmenu; import android.content.Intent; import android.os.Bundl ...

  3. ZOJ 3778 Talented Chef 模拟 [ 祝愿明天省赛一帆风顺, ZJSU_Bloom WILL WIN : )

    这题的意思是给你 n 道菜,第 i 道菜需要 Ai 步才能完成 每次你能对 m 道菜分别完成一步,请问最少需要几次? 这题暴力写肯定是不行的,去年省赛的时候就是没写出来这题,今天再把思路理一理吧. 首 ...

  4. 【集训笔记】动态规划背包问题【HDOJ1421【HDOJ1058【HDOJ2546

    http://acm.hdu.edu.cn/showproblem.php?pid=2546 http://acm.zju.edu.cn/onlinejudge/showContestProblem. ...

  5. synapse socket总结一:服务器模型

    synapse (http://synapse.ararat.cz/doku.php)的源码简洁明了,属于轻量级的阻塞式socket通讯组件包,更多的功能需要自己基于它的基础上去封装实现.相对于ind ...

  6. php三元运算

    $a = 2; $a == 1 ? $test="企业" : ($a==2 ? $test="地区" : $test="其他地方"); ec ...

  7. 基于visual Studio2013解决算法导论之028散列表开放寻址

     题目 散列表 解决代码及点评 #include <iostream> #include <time.h> using namespace std; template & ...

  8. 项目中用到的Java注解

    元注解: @Retention(RetentionPolicy.RUNTIME)   @Target({ElementType.METHOD})  作用:@interface用来声明一个注解,其中的每 ...

  9. Hadoop: the definitive guide 第三版 拾遗 第十章 之Pig

    概述: Pig的安装很简单,注意一下几点: 1.设置系统环境变量: export PIG_HOME=.../pig-x.y.z export PATH=$PATH:$PIG_HOME/bin 设置完成 ...

  10. erlang 初体验

    近期測试了一下 erlang的坑... 如不出意外.... 大家第一眼看到这语法... 心里第一句一定是"我擦.这TM都是啥!!!!!" 没有变量!!! 没有结构体!!! 没有循环 ...