1

Hey!

I now try to send packet with boost asio, and i store packet data in boost array. Before i was sending it by dynamic allocated char array.

I declare array by boost::array<char, 5824>

And after it I set normally values in my array.

I send it by boost asio (synchronously). But problem is there when client receive packet, about 1000 first bytes are ok, but other contains random data.

What I doing there wrong ? I tried different combinations, but result is same. Thanks!

edit

I fill up data by memcpy on array pointer obtained by method array.data();
For example memcpy(array.data()+10, &chararray, 15);
Data is sended by boost::asio::write(socket_, boost::asio::buffer(array));

7
  • How are you sending the data? Commented Jan 4, 2014 at 15:09
  • 1
    Please show us the code you use to fill and send the array. If possible, please provide a SSCCE Commented Jan 4, 2014 at 15:10
  • I added example of filling it with data, and send function. Commented Jan 4, 2014 at 15:15
  • @Mgetz thank you for help! When i changed it into std::array it started working ;). But it seems it still have random bytes at all. Commented Jan 4, 2014 at 15:28
  • @user3126089 I've made my comment into an answer Commented Jan 4, 2014 at 15:52

1 Answer 1

2

Why not use std::array? Boost recommends it as a replacement given that it's been added to the standard library. Then you can use the fill member to initialize it, or use a {} initalizer to zero it in place.

using socketData = std::array<char, 5824>;
socketData data{}; // uninitialized per standard, the {} zeros

socketData::iterator iter = data.begin();

// write data using iterators
boost::asio::write(socket_, boost::asio::buffer(array));
Sign up to request clarification or add additional context in comments.

3 Comments

You don't need to fill with zeros. You can say socketData data{};. But your code is not doing the same as OP's. They are copying from a different array.
@juanchopanza I've updated my example for your zeroing recommendation. I'm not seeing their copy but that would be easily achieved using std::copy anyway.
They memcpy 15 bytes from one array to another, starting at the 10th position of the destination array. Yes, you can do that with std::copy and it would be easier to read :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.