sending struct of int from Linux to Windows using boost::asio

Hello All, I am having trouble using boost:asio to send a simple struct message_header from server (linux) to the client (windows). typedef struct MessageHeader { uint32_t _data_length; //length of data attached uint8_t _message_type; //MESSAGE_TYPE uint8_t _segment_name; //SEGMENT_NAME uint16_t _request_id; //Request Id assigned by the client to the request to uniquely identify the request uint16_t _count_of_records; //Number of records affected by the operation, 0 means complete segment is affected. } MessageHeader; typedef struct Message { boost::array<char, header_length> _header_array; } Message Server side code (Linux): =============== MessageHeader request_header; request_header._data_length = 0; request_header._message_type = MSG_READ; request_header._segment_name = SEG_OOW; request_header._count_of_records = 0; request_header._request_id = 100; string _data_serialized = ""; uint8_t header[header_length]; //header_length is defined as 10 memcpy(header, (uint8_t*)(&(request_header)), header_length); std::vector<boost::asio::const_buffer> buffers; buffers.push_back(boost::asio::buffer(header, header_length)); buffers.push_back(boost::asio::buffer((_data_serialized).c_str(), _data_serialized.length())); boost::asio::async_write(_socket, buffers, boost::bind(&ClientSession::handle_write, shared_from_this(), boost::asio::placeholders::error)); Client Side code (Windows): ================ Message _read_message; MessageHeader _message_header; Handle_connect(....) { boost::asio::async_read(_socket, boost::asio::buffer(_read_msg._header_array, header_length), boost::bind(&DJClient::Handle_read_header, this, boost::asio::placeholders::error)); } Handle_read_header(...) { memcpy(&_message_header, read_msg._header_array.data(), header_length); } Surprisingly the same code works fine from "Windows to Windows" and "Windows to linux" but has problem when used on "Linux to Windows". When sent from Linux server, Windows server receives garbage data. Sending of normal ASCII characters from Linux to Windows work fine. Please help if anyone can. Thanks and Regards, Anil Agrawal

A.Agrawal@patrick.com.au wrote: [snip]
buffers.push_back(boost::asio::buffer((_data_serialized).c_str(), _data_serialized.length()));
[snip] I haven't read all of your code, but I don't think .c_str() will have an object lifetime that exceeds the asynchronous operation. There's support for std::string, you could try buffers.push_back(boost::asio::buffer(_data_serialized)); Have you verified that on your platform sizeof(MessageHeader) equals what you have set for it? Kind regards, Rutger ter Borg

On Thu, 10 Sep 2009 04:50:59 +0200, <A.Agrawal@patrick.com.au> wrote:
Hello All,
I am having trouble using boost:asio to send a simple struct message_header from server (linux) to the client (windows).
Do you deal correctly with the endianness (http://en.wikipedia.org/wiki/Endianness) by using htonl and the like?

Also look at asio+serialization example: http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/examples.html
participants (4)
-
A.Agrawal@patrick.com.au
-
Bjarne Laursen
-
Igor R
-
Rutger ter Borg