Receive message using boost::asio raw_socket

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!

[Receiver code]

int main(int argc, const char **argv) {

....

namespace asio = boost::asio;
asio::io_service io_service;

asio::generic::raw_protocol raw_protocol(AF_PACKET, htons(ETH_P_ALL));
asio::generic::raw_protocol::socket raw_socket(io_service, raw_protocol);
raw_socket.bind(dev.endpoint(AF_PACKET));

std::vector<asio::const_buffer> receiveBuffer;
raw_socket.receive_from(receiveBuffer, dev.endpoint(AF_PACKET));

...

}
Last edited on
Take a look at this example for how to use the buffer class:

https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/example/cpp11/spawn/echo_server.cpp

Particular:

1
2
3
4
5
            char data[128];
            for (;;)
            {
              timer_.expires_from_now(std::chrono::seconds(10));
              std::size_t n = socket_.async_read_some(boost::asio::buffer(data), yield);
Hi, thanks for your feedback.

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);
What does 'doesn't work' mean, exactly?

The buffer must be mutable and requires am underlying real buffer. I suggest that you try as the example above shows.
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).
Last edited on
I did not mean that you should use async_read_some(...). It was just about the buffer. Try this:

1
2
char data[128];
auto bytes_received = raw_socket.receive(boost::asio::buffer(data));


Actually you may change the size of the buffer.
Last edited on
Hi, thank you for your feedback.

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?

-----------------------Tx code----------------------
char data[20] = "Hello World!";
auto bytes_sent = socket_.send(asio::buffer(data));

-----------------------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!
Can you elaborate on the problem?
Last edited on
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! :)
Topic archived. No new replies allowed.