原文: http://blog.csdn.net/byxdaz/article/details/79244800

这个在boost1.64下是报错的,

boost::array<char,TCP_RECV_DATA_PACKAGE_MAX_LENGTH> m_rbTempRecvBuffer;

报不完整的类型,把boost去掉,引用std的array,异常就消失了

异步不能连接回调

tcpclient.h

[cpp] view plaincopy
  1. #pragma once
  2. #include <boost/function.hpp>
  3. #include <boost/bind.hpp>
  4. #include <boost/asio.hpp>
  5. #include <boost/shared_ptr.hpp>
  6. #include <iostream>
  7. #include <vector>
  8. #include <string>
  9. using namespace boost::asio;
  10. #define         TCP_RECV_DATA_PACKAGE_MAX_LENGTH                1024
  11. //接收数据类型
  12. typedef enum RecvDataType
  13. {
  14. RecvDataType_NoKnown        =       0x00,           //未知
  15. RecvDataType_Head           =       0x01,           //头数据
  16. RecvDataType_Body           =       0x02,           //体数据
  17. RecvDataType_Some           =       0x03,           //部分数据
  18. RecvDataType_OnePackage     =       0x04,           //整包数据
  19. };
  20. //发送数据回调函数
  21. typedef void (CALLBACK *TcpSendDataCallback)(const boost::system::error_code& error,std::size_t bytes_transferred,DWORD dwUserData1,DWORD dwUserData2);
  22. //接收数据回调函数
  23. typedef void (CALLBACK *TcpRecvDataCallback)(const boost::system::error_code& error,char *pData,int nDataSize,DWORD dwUserData1,DWORD dwUserData2);
  24. class TcpClient
  25. {
  26. public:
  27. TcpClient(void);
  28. virtual ~TcpClient(void);
  29. //设置参数
  30. void    SetParameter(unsigned int uiSendBufferSize,unsigned int uiSendTimeout,unsigned int uiRecvBufferSize,unsigned int uiRecvTimeout);
  31. //设置阻塞与非阻塞
  32. void    SetNoBlock(bool bNoBlock);
  33. //连接服务器(同步)
  34. int     ConnectServer(char *pIp,unsigned short usPort,unsigned int uiConnectTimeout);
  35. //连接服务器(异步)
  36. int     ConnectServerByAynsc(char *pIp,unsigned short usPort,unsigned int uiConnectTimeout,unsigned int uiReconnectInteralTime);
  37. //关闭连接
  38. void    CloseConnect();
  39. //发送数据
  40. int     SendData(char *pBuffer,int nBufferSize);
  41. //接收数据
  42. int     RecvData(char *pBuffer,int nBufferSize);
  43. //接收数据(阻塞)
  44. int     RecvDataByBlock(char *pBuffer,int nBufferSize);
  45. //发送数据(异步)
  46. int     SendDataByAynsc(char *pBuffer,int nBufferSize,TcpSendDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2);
  47. //接收数据(异步)
  48. int     RecvDataByAynsc(TcpRecvDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2);
  49. protected:
  50. void connect_handler(const boost::system::error_code& ec);
  51. void async_read_handler(const boost::system::error_code& ec,size_t bytes_transferred,TcpRecvDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2);
  52. void read_hander(char *pBuffer,size_t bytes_transferred,const boost::system::error_code& err);
  53. void write_handler(const boost::system::error_code& error,size_t bytes_transferred,TcpSendDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2);
  54. void RecvDataTimeoutProcess();
  55. io_service m_io;
  56. ip::tcp::endpoint * m_pEndPoint;
  57. ip::tcp::socket * m_pSocket;
  58. std::array<char, TCP_RECV_DATA_PACKAGE_MAX_LENGTH> m_rbTempRecvBuffer;       //临时接收数据缓冲区

    //boost::array<char,TCP_RECV_DATA_PACKAGE_MAX_LENGTH> m_rbTempRecvBuffer;       //临时接收数据缓冲区

  59. int m_nSyncRecviceDataSize;             //同步接收数据大小
  60. unsigned int m_uiSendBufferSize;
  61. unsigned int m_uiSendTimeout;
  62. unsigned int m_uiRecvBufferSize;
  63. unsigned int m_uiRecvTimeout;
  64. deadline_timer * m_pTimer;
  65. };

tcpclient.cpp

[cpp] view plaincopy
  1. #include "StdAfx.h"
  2. #include "TcpClient.h"
  3. TcpClient::TcpClient(void)
  4. {
  5. m_uiSendBufferSize = 0;
  6. m_uiSendTimeout = 10000;
  7. m_uiRecvBufferSize = 0;
  8. m_uiRecvTimeout = 10000;
  9. m_pEndPoint = NULL;
  10. m_pSocket = NULL;
  11. m_nSyncRecviceDataSize = 0;
  12. m_pTimer = new deadline_timer(m_io);
  13. }
  14. TcpClient::~TcpClient(void)
  15. {
  16. }
  17. //设置参数
  18. void    TcpClient::SetParameter(unsigned int uiSendBufferSize,unsigned int uiSendTimeout,unsigned int uiRecvBufferSize,unsigned int uiRecvTimeout)
  19. {
  20. m_uiSendBufferSize = uiSendBufferSize;
  21. m_uiSendTimeout = uiSendTimeout;
  22. m_uiRecvBufferSize = uiRecvBufferSize;
  23. m_uiRecvTimeout = uiRecvTimeout;
  24. if(m_uiRecvTimeout <= 0)
  25. {
  26. m_uiRecvTimeout = 10000;
  27. }
  28. }
  29. //连接服务器(同步)
  30. int     TcpClient::ConnectServer(char *pIp,unsigned short usPort,unsigned int uiConnectTimeout)
  31. {
  32. if(pIp == NULL || usPort == 0)
  33. return -1;
  34. try
  35. {
  36. m_pEndPoint = new ip::tcp::endpoint(ip::address::from_string(pIp), usPort);
  37. m_pSocket = new ip::tcp::socket(m_io);
  38. m_pSocket->open(m_pEndPoint->protocol());
  39. m_pSocket->set_option(boost::asio::ip::tcp::socket::reuse_address(true));
  40. if(m_uiSendBufferSize != 0)
  41. {
  42. boost::asio::socket_base::send_buffer_size sendBufferSize(m_uiSendBufferSize);
  43. m_pSocket->set_option(sendBufferSize);
  44. }
  45. if(m_uiRecvBufferSize != 0)
  46. {
  47. boost::asio::socket_base::receive_buffer_size recvBufferSize(m_uiRecvBufferSize);
  48. m_pSocket->set_option(recvBufferSize);
  49. }
  50. //connect
  51. m_pSocket->connect(*m_pEndPoint);
  52. /*
  53. char str[1024];
  54. sock.read_some(buffer(str));
  55. std::cout << "receive from" << sock.remote_endpoint().address() << std::endl;;
  56. std::cout << str << std::endl;
  57. */
  58. }
  59. catch (std::exception& e)
  60. {
  61. std::cout << e.what() << std::endl;
  62. return -2;
  63. }
  64. return 0;
  65. }
  66. //连接服务器(异步)
  67. int TcpClient::ConnectServerByAynsc(char *pIp,unsigned short usPort,unsigned int uiConnectTimeout,unsigned int uiReconnectInteralTime)
  68. {
  69. if(pIp == NULL || usPort == 0)
  70. return -1;
  71. try
  72. {
  73. m_pEndPoint = new ip::tcp::endpoint(ip::address::from_string(pIp), usPort);
  74. m_pSocket = new ip::tcp::socket(m_io);
  75. m_pSocket->open(m_pEndPoint->protocol());
  76. m_pSocket->set_option(boost::asio::ip::tcp::socket::reuse_address(true));
  77. if(m_uiSendBufferSize != 0)
  78. {
  79. boost::asio::socket_base::send_buffer_size sendBufferSize(m_uiSendBufferSize);
  80. m_pSocket->set_option(sendBufferSize);
  81. }
  82. if(m_uiRecvBufferSize != 0)
  83. {
  84. boost::asio::socket_base::receive_buffer_size recvBufferSize(m_uiRecvBufferSize);
  85. m_pSocket->set_option(recvBufferSize);
  86. }
  87. //connect
  88. m_pSocket->async_connect(*m_pEndPoint,boost::bind(&TcpClient::connect_handler,this, boost::asio::placeholders::error));
  89. }
  90. catch (std::exception& e)
  91. {
  92. std::cout << e.what() << std::endl;
  93. return -2;
  94. }
  95. return 0;
  96. }
  97. void TcpClient::connect_handler(const boost::system::error_code& ec)
  98. {
  99. if (ec)
  100. {
  101. return;
  102. }
  103. std::cout << "receive from:" << m_pSocket->remote_endpoint().address() << std::endl;
  104. }
  105. void TcpClient::async_read_handler(const boost::system::error_code& ec,size_t bytes_transferred,TcpRecvDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2)
  106. {
  107. //回调数据
  108. if(fnCallback != NULL)
  109. {
  110. fnCallback(ec,m_rbTempRecvBuffer.data(),bytes_transferred,dwUserData1,dwUserData2);
  111. }
  112. if(ec == boost::asio::error::eof)
  113. {
  114. //对端方关闭连接
  115. if(m_pSocket->is_open())
  116. {
  117. m_pSocket->close();
  118. }
  119. //printf("close connect \n");
  120. return;
  121. }
  122. if(ec != NULL)
  123. {
  124. //发送数据失败
  125. return;
  126. }
  127. //接收下一条数据
  128. m_pSocket->async_read_some(boost::asio::buffer(m_rbTempRecvBuffer),
  129. boost::bind(&TcpClient::async_read_handler, this,
  130. boost::asio::placeholders::error,
  131. boost::asio::placeholders::bytes_transferred,
  132. fnCallback,dwUserData1,dwUserData2));
  133. }
  134. //关闭连接
  135. void    TcpClient::CloseConnect()
  136. {
  137. if(m_pSocket != NULL)
  138. {
  139. m_pSocket->close();
  140. m_pSocket = NULL;
  141. }
  142. }
  143. //发送数据
  144. int     TcpClient::SendData(char *pBuffer,int nBufferSize)
  145. {
  146. int nRet = 0;
  147. if(m_pSocket != NULL)
  148. {
  149. nRet = m_pSocket->send(buffer(pBuffer,nBufferSize));
  150. }
  151. return nRet;
  152. }
  153. //接收数据
  154. int     TcpClient::RecvData(char *pBuffer,int nBufferSize)
  155. {
  156. int nRet = 0;
  157. if(m_pSocket != NULL)
  158. {
  159. m_nSyncRecviceDataSize = 0;
  160. boost::system::error_code ec;
  161. m_pSocket->async_read_some(buffer(pBuffer,nBufferSize),boost::bind(&TcpClient::read_hander,this,pBuffer,boost::asio::placeholders::bytes_transferred,boost::asio::placeholders::error));
  162. m_pTimer->expires_from_now(boost::posix_time::seconds(m_uiRecvTimeout));
  163. m_pTimer->async_wait(boost::bind(&TcpClient::RecvDataTimeoutProcess,this));
  164. m_io.reset();
  165. m_io.run(ec);
  166. nRet = m_nSyncRecviceDataSize;
  167. }
  168. return nRet;
  169. }
  170. void TcpClient::read_hander(char *pBuffer,size_t bytes_transferred,const boost::system::error_code& err)
  171. {
  172. if (err)
  173. {
  174. return;
  175. }
  176. m_nSyncRecviceDataSize = bytes_transferred;
  177. m_pTimer->cancel();
  178. }
  179. void TcpClient::RecvDataTimeoutProcess()
  180. {
  181. int n = 0;
  182. }
  183. //接收数据(阻塞)
  184. int TcpClient::RecvDataByBlock(char *pBuffer,int nBufferSize)
  185. {
  186. int nRet = 0;
  187. if(m_pSocket != NULL)
  188. {
  189. m_nSyncRecviceDataSize = 0;
  190. boost::system::error_code ec;
  191. m_pSocket->receive(buffer(pBuffer,nBufferSize));
  192. }
  193. return nRet;
  194. }
  195. //发送数据(异步)
  196. int     TcpClient::SendDataByAynsc(char *pBuffer,int nBufferSize,TcpSendDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2)
  197. {
  198. if(pBuffer == NULL || nBufferSize == 0)
  199. return -1;
  200. if(m_pSocket == NULL || !m_pSocket->is_open())
  201. return -1;
  202. boost::asio::async_write(
  203. *m_pSocket,
  204. boost::asio::buffer(pBuffer,nBufferSize),
  205. boost::bind(&TcpClient::write_handler, this,
  206. boost::asio::placeholders::error,
  207. boost::asio::placeholders::bytes_transferred,
  208. fnCallback,dwUserData1,dwUserData2));
  209. return 0;
  210. }
  211. //接收数据(异步)
  212. int     TcpClient::RecvDataByAynsc(TcpRecvDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2)
  213. {
  214. if(m_pSocket == NULL || !m_pSocket->is_open())
  215. return -1;
  216. m_pSocket->async_read_some(buffer(m_rbTempRecvBuffer),boost::bind(&TcpClient::async_read_handler, this, boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred,fnCallback,dwUserData1,dwUserData2));
  217. return 0;
  218. }
  219. //设置阻塞与非阻塞
  220. void TcpClient::SetNoBlock(bool bNoBlock)
  221. {
  222. if(m_pSocket == NULL)
  223. return;
  224. if(bNoBlock)
  225. {
  226. boost::asio::ip::tcp::socket::non_blocking_io io_option(true);
  227. m_pSocket->io_control(io_option);
  228. }
  229. else
  230. {
  231. //阻塞
  232. boost::asio::ip::tcp::socket::non_blocking_io io_option(false);
  233. m_pSocket->io_control(io_option);
  234. }
  235. }
  236. void TcpClient::write_handler(const boost::system::error_code& error,size_t bytes_transferred,TcpSendDataCallback fnCallback,DWORD dwUserData1,DWORD dwUserData2)
  237. {
  238. if(fnCallback != NULL)
  239. {
  240. fnCallback(error,bytes_transferred,dwUserData1,dwUserData2);
  241. }
  242. if(error == boost::asio::error::eof)
  243. {
  244. //对端方关闭连接
  245. if(m_pSocket->is_open())
  246. {
  247. m_pSocket->close();
  248. }
  249. //printf("close connect \n");
  250. return;
  251. }
  252. if(error != NULL)
  253. {
  254. //发送数据失败
  255. return;
  256. }
  257. #ifdef _DEBUG
  258. //写数据
  259. printf("write data!!!\n");
  260. #endif
  261. }

boost库之tcp client 回调不正常相关推荐

  1. boost库之tcp server(异步)

    原文:http://blog.csdn.net/byxdaz/article/details/72676000 //服务端 boost1.64 报错: boost::array<char, 12 ...

  2. boost库之tcp实例(同步方式)

    原文:http://blog.csdn.net/byxdaz/article/details/72627678 //服务端 [cpp] view plaincopy #include <iost ...

  3. 【Boost】boost库asio详解9——TCP的简单例子2

    客户端: // Client.cpp : 定义控制台应用程序的入口点. //#include "stdafx.h" #include <iostream> #inclu ...

  4. 【Boost】boost库asio详解8——TCP的简单例子1

    摘于boost官网的几个例子, 做了点小修改, 笔记之. 同步客户端 [cpp] view plain copy  print? void test_asio_synclient() { typede ...

  5. boost asio 文件服务器,使用boost ASIO库封装TCP服务器类

    使用异步TCP方式,可在此基础上增加更多功能. 头文件AsioTcp.h: #pragma once #include #include #include typedef boost::asio::i ...

  6. boost库中优秀的网络库asio

    文章目录 一.须知 二.ASIO 三.我们将从研究同步操作开始 四.当使用异步操作时,会发生不同的事件序列 五.Proactor模型 六.常用内容 七.C++ 建立本地网络服务器 (Boost.Asi ...

  7. ESP8266基于MicroPython的TCP socket回调函数实现案例

    参考链接 其它参考 python socket和简单tcp通信实现 - Katherina.K - 博客园 GitHub - robert-hh/FTP-Server-for-ESP8266-ESP3 ...

  8. boost库使用—asio库

    boost库使用-asio库 **** 一.boost库asio简介 Boost Asio ( asynchronous input and output)关注异步输入输出.Boost Asio库提供 ...

  9. boost库之socket 非阻塞/缓冲区大小等属性设置

    boost库之socket 非阻塞/缓冲区大小等属性设置 原文:http://blog.csdn.net/byxdaz/article/details/77318219 asio socket 非阻塞 ...

最新文章

  1. python 统计分析apache日志_python切分apache日志文件
  2. LNOI2014 LCA
  3. C++学习之普通函数指针与成员函数指针
  4. python数码时钟代码_python实现简易数码时钟
  5. salt-api安装与配置
  6. DLL程序组件Microsoft Reporting Services Barcode Custom Report Item
  7. 107 岁的 IBM 以 340 亿美元吞下了 25 岁的“小”红帽!
  8. mongoDB学习--建库、删库、插入、更新
  9. 直方图、正态分布图与SPC图
  10. 云南机房建设整体解决方案、华为智能模块化数据中心机房
  11. WindowsBuilder控件中文编码问题
  12. 基于pyswarm库实现粒子群优化算法求解带约束的优化问题
  13. 【2019-06-27】现在不杂
  14. 三层交换机光模块故障排除流程
  15. dbf解析_JAVA解析DBF文件方案.pdf
  16. C语言写出猜数字游戏的代码
  17. php去除头尾空格,php去除头尾空格的2种方法,php头尾空格2种_PHP教程
  18. 448. Find All Numbers Disappeared in an Array(找到所有数组中消失的数字)
  19. 第一章 信息资源管理基础
  20. 阿里云实战之一(必备程序安装)

热门文章

  1. html根据文档定位,html文档中的location对象属性理解及常见的用法
  2. linux删除文件夹命令6,linux 结合find命令进行文件的删除
  3. 读取list java_java 分批次读取java.util.List 数据
  4. 运行python程序ModuleNotFoundError?pip下载速度太慢?
  5. 机器人 海难饥荒_饥荒:这个人物后期轻松单挑Boss,缺点却很小,大力士都比不过...
  6. java中友元类_友元类成员的依赖关系|循环依赖
  7. 查题接口 源码 php 爬题,大学网课查题公众号查题教程_网课查题题库接口API-在线搜答案...
  8. 计算机管理器中没有停止共享,域客户端默认共享关闭讨论.
  9. list.php tid= field,织梦CMS初试-套用指定的html模板,解析内容列表【笔记】
  10. 笑死!“盒马”把自己的ID给玩没了...