1

So my setup is the following:

  • Raspberry Pi working as a TCP server with boost asio c++ library;
  • TCP Client running on other machine;

The communication is working fine expect for the part that I should be able to send an answer to the client request. To do so I'm using the following code:

std::cout << "\tI2C message from Arduino: " << I2CrxBuf_;
boost::asio::async_write(sock_, boost::asio::buffer( I2CrxBuf_.c_str(), sizeof(I2CrxBuf_.c_str()) ), boost::bind(&conn::h_write, shared_from_this()));
std::cout << "Passei o async_write" << std::endl;

The thing is that the message prints just fine but then it jumps to the last print without sending the message to the client, and so the client blocks.

The output in the server is the following:

I2C message from Arduino: l 1 14.88
Passei o async_write

If I send a generic message like this:

boost::asio::async_write(sock_, boost::asio::buffer( "Message recevied\n" ), boost::bind(&conn::h_write, shared_from_this()));

The client receives the message as it was expected.

I'm pretty sure the problem as to do with the way I'm converting the string to char*, but I not finding a way to make it work.

6
  • 1
    sizeof(I2CrxBuf_.c_str()) is 4 bytes. I think you meant I2CrxBuf_.size().
    – arrowd
    Commented Dec 9, 2017 at 17:55
  • Thanks, that true but still not sending...
    – Sebastião
    Commented Dec 9, 2017 at 17:57
  • Also I have checked and the c.str() method does not removed the '\n ' from the end of the string. Since the client needs the message to have it this should not be the problem.
    – Sebastião
    Commented Dec 9, 2017 at 18:02
  • 1
    Not quite sure I understand the problem, but... the value returned from std::string::c_str should really be treated as a temporary and, thus, isn't suitable for passing to an asynchronous function such as async_write. If I2CrxBuf_ is in scope and unmodified for the duration of the write operation then just construct the buffer using that -- boost::asio::buffer(I2CrxBuf_) instead.
    – G.M.
    Commented Dec 9, 2017 at 18:11
  • Sorry @arrowd that was indeed the problem.
    – Sebastião
    Commented Dec 9, 2017 at 18:13

1 Answer 1

2

sizeof(I2CrxBuf_.c_str()) is wrong. Also, you can do buffer(I2CrxBuf) directly. See docs for other overloads.

Other than that, realize that the string needs to stay alive until the end of the async operation (and cannot be modified in the mean time).

All the samples in the documentation have good ideas on how to achieve this.

5
  • Thaks! I have mutex’s to make sure that it doesn’t get modified... but anyway thanks for the warning!
    – Sebastião
    Commented Dec 9, 2017 at 20:27
  • 1
    Mutex can not really work in asynchronous settings. Of course it can if you jump through hoops, make sure all handlers run on a specific thread and you keep the mutex locked for long durations (all of these are design smells) - I really think we can be 90% sure your mutex approach is flawed. How do you keep the string alive? Look at Asio Strands about synchronization on data/resources
    – sehe
    Commented Dec 9, 2017 at 20:30
  • Sorry, In this case I'm using condition_variable... They should do the job right?
    – Sebastião
    Commented Dec 9, 2017 at 20:32
  • No difference. condition_variable implies a locking primitive. I'm happy to review real code. In case you haven't heard about this site: Code Review
    – sehe
    Commented Dec 9, 2017 at 20:41
  • The thing here is that I need the server to stop while I get the answer to the client request on another thread. Is there a better way to do it? Btw thanks for the link I will give it a check.
    – Sebastião
    Commented Dec 9, 2017 at 20:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.