Is there anyway to BASICALLY do the following:
#include <boost/asio.hpp>
struct testStruct{
int x;
int y;
};
int main(){
struct testStruct t;
boost::asio::buffer b;
b = boost::asio::buffer(t);
return 0;
}
Where it seems to fail is passing 't' into the buffer, 'b'.
Use the scatter operation of more than a single buffer:
#include <boost/asio.hpp>
#include <vector>
struct testStruct{
int x;
int y;
};
int
main()
{
struct testStruct t;
t.x = 5;
t.y = 7;
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back( boost::asio::buffer(&t.x, sizeof(t.x) ) );
buffers.push_back( boost::asio::buffer(&t.y, sizeof(t.y) ) );
boost::asio::io_service io_service;
boost::asio::ip::tcp::socket socket( io_service ); // note not connected!
std::size_t length = boost::asio::write( socket, buffers );
return 0;
}
Note you'll need to use a corresponding gather on the receiving side. This gets very tedious with anything more than the contrived example you have presented. Which is why I suggested using a more robust serialization mechanism in your previous question.
Just Use Boost.Serialization You can get a demo from the http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/examples.html
When you want to send an Object, It is better for you to Serialize it First.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With