如何读取使用升压ASIO一个固定大小的数据包? [英] How to read a fix-sized packet using boost asio?

查看:189
本文介绍了如何读取使用升压ASIO一个固定大小的数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个的同步读/使用升压ASIO写。数据是以二进制格式的到来,没有边界,长度信息连接的数据包格式codeD。因此,它具有指定大小阅读是很重要的。可 IP :: TCP :: iostream的做到这一点?有人可以提供一个例子吗?谢谢你。

I am doing a synchronous read/write using boost-asio. The data is coming in binary format, without boundary, the length information is encoded in the packet format. So it is important to read in with specified size. Can ip::tcp::iostream do that? Can someone provide an example? Thanks.

推荐答案

我在一个程序中工作至极不同尺寸发送不同的数据。我用8个字节的固定头连接code尺寸,然后,我添加数据:

I work on a program wich send different data with different size. I use a fixed header of 8 byte to encode the size, then, I add the data :

 enum { header_length = 8 }; //const header length

我得到的大小(m_outbound_data是为std :: string ==序列化对象)

I get the size (m_outbound_data is a std::string == a serialized object)

//give header length    
std::ostringstream header_stream
header_stream << std::setw(header_length) //set a field padding for header  
              << std::hex                 //set next val to hexadecimal
              << m_data_out.m_outbound_data.size(); //write size in hexa

m_data_out.m_outbound_header = header_stream.str(); //m_outbound_head == size in hexa in a std::string

      //m_outbound_header = [ 8 byte size ] 
      //m_outbound_data = [ serialized data ]

      //write all data in the std::vector and send it
      std::vector<boost::asio::const_buffer> buffer;
      buffer.push_back(boost::asio::buffer(m_data_out.m_outbound_header));
      buffer.push_back(boost::asio::buffer(m_data_out.m_outbound_data));

和读取,需要在2次读取:第一个读8个字节​​的拿到尺寸,然后读取的数据以矢量和反序列化到对象:

And for reading, you need to read in 2 time : 1st read 8 byte to get the size, then read the data in a vector and deserialize into object :

 struct network_data_in {   
  char m_inbound_header[header_length]; //size of data to read  
  std::vector<char> m_inbound_data; // read data    
};

我用这个结构来获取数据,调用read在m_inbound_header先填充大小缓冲区,然后,在手柄:

I use this struct to get data, call read on the m_inbound_header to fill the buffer with size first, then, in the handle :

//get size of data
std::istringstream is(std::string(m_data_in.m_inbound_header, header_length));
std::size_t m_inbound_datasize = 0;
is >> std::hex >> m_inbound_datasize;
m_data_in.m_inbound_data.resize(m_inbound_datasize); //resize the vector

然后再调用与缓冲区m_inbound_data,恰好读取数据这一结果发送读
在您探微第二handle_read有反序列化数据:

then call again read with the m_inbound_data on buffer, this result of reading exactly the data sent In the second handle_read you juste have to deserialize the data :

//extract data
std::string archive_data (&(m_data_in.m_inbound_data[0]),m_data_in.m_inbound_data.size());
std::istringstream archive_stream(archive_data);
boost::archive::text_iarchive archive(archive_stream);
archive >> t; //deserialize

希望能够帮助您!

Hope that help you !

这篇关于如何读取使用升压ASIO一个固定大小的数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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