Hi! I need help in using raw_socket of boost asio.
The goal is to transmit and receive messages using raw_socket. My current setup is that, I have separate codes for the transmit side and receiver side, both of which I plan to run on the same host. The transmit side will send using the loopback interface, and the receive side will listen and receive also using the loopback interface.
A basic_raw_socket has already been created and opened. It has also been bound to the local endpoint as in the code below. However, I'm have some errors when trying to create the receiveBuffer and configuring it to listen and receive the message. Could you please help me with this? Thank you!
I'm assuming I could only use the options included in basic_raw_socket.hpp (which are receive, receive_from, async_receive and async_receive_from).
I tried the following code, but it still doesn't work.
asio::const_buffer receiveBuffer;
auto bytes_received = raw_socket.receive(receiveBuffer);
Hi, yes you're right. I must use mutable_buffer.
I tried doing that as in the code below, but it seems that I'm doing it incorrectly as I'm getting the following error.
--------------------------------------------------------------------------------------------------
[Code]
asio::mutable_buffer receiveBuffer;
auto bytes_received = raw_socket.receive(receiveBuffer);
--------------------------------------------------------------------------------------------------
[Error]
/usr/include/boost/asio/detail/buffer_sequence_adapter.hpp:107:38: error: no type named ‘const_iterator’ in ‘class boost::asio::mutable_buffer’
typename Buffers::const_iterator iter = buffer_sequence.begin();
^~~~
/usr/include/boost/asio/detail/buffer_sequence_adapter.hpp:108:38: error: no type named ‘const_iterator’ in ‘class boost::asio::mutable_buffer’
typename Buffers::const_iterator end = buffer_sequence.end();
^~~
/usr/include/boost/asio/detail/buffer_sequence_adapter.hpp:107:38: error: no type named ‘const_iterator’ in ‘class boost::asio::mutable_buffer’
typename Buffers::const_iterator iter = buffer_sequence.begin();
^~~~
/usr/include/boost/asio/detail/buffer_sequence_adapter.hpp:108:38: error: no type named ‘const_iterator’ in ‘class boost::asio::mutable_buffer’
typename Buffers::const_iterator end = buffer_sequence.end();
^~~
--------------------------------------------------------------------------------------------------
Also, I already tried the example you suggested, but it seems that I cannot use async_read_some with raw_socket (with reference to basic_raw_socket.hpp).
Unfortunately, after several tries, I'm still not successful in transmitting then receiving.
Could you please check my code below if I'm missing something?
-----------------------Rx code----------------------
auto bytes_received = socket_.receive(asio::buffer(receive_buffer_));
for (unsigned i = 0; i < bytes_received; ++i)
std::cout << receive_buffer_[i];
If this is correct then I might have to check my code for network programming.
Thanks again!
Hi, it seems that the syntax for the buffer is now OK. Thank you for your help! I think the problem now is related to socket programming. I could verify in Wireshark that the message is being sent correctly, so the problem is on the receiver side as it is not receiving anything. I'm still going through the code to figure out if I made a mistake when configuring the socket in the receiver code. In the meantime, I think we can close this issue. Thanks again! :)