C ++: does boost :: asio: write () not support UDP sockets?

Trying to use boost::asio::write()to write to an object boost::asio::ip::udp::socket. While I'm busy re-reading the documents to see if I have arrived somewhere, can someone confirm, maybe this is not supported? Now I think it boost::asio::write()only supports objects tcp::socket, not udp::socket.

This is what I get when I try to compile:

/usr/include/boost/asio/impl/write.ipp: In function ‘size_t
boost::asio::write(SyncWriteStream&, const ConstBufferSequence&, [...cut...]
test/test.cpp:76:   instantiated from here
/usr/include/boost/asio/impl/write.ipp:44: error:class
boost::asio::basic_datagram_socket<boost::asio::ip::udp,
boost::asio::datagram_socket_service<boost::asio::ip::udp> >’ has no member named
‘write_some’

I think I decided to try boost::asio::write()it when I read this in the docs:

The send operation may not transfer all data to the partner. Consider using the write function if you need to make sure that all data is written before the completion of the lock operation.

... but coming back, I see that the text is only in boost::asio::ip::tcp::socket::send(), not in the UDP version. Source

+3
source share
3 answers

Assuming you want synchronous behavior, you need to use the send or send_to class methods boost::asio::ip::udp::socket. A free function boost::asio::writeuses a requirement of type SyncWriteStream that does not match a UDP socket.

+4
source

Instead, I use async_send_to. Perhaps this is possible for you?

void MyUdpServer::sendMessage(Message& message)
{
    m_message = message;

    m_socket.async_send_to(
        asio::buffer(&m_message, sizeof(m_message)), m_endpoint,
        boost::bind(&MyUdpServer::handle_send_to, shared_from_this(), 
        asio::placeholders::error));
} 

where m_socketis asio::ip::udp::socket.

+1
source

, asio:: write , stream write_some. IIRC. UDP , ( + UDP- + IP- PMTU, - ).

Do you have host code that reorders / collects UDP packets? You will receive UDP packets out of order and fragmented no matter how good your network is.

0
source

Source: https://habr.com/ru/post/1783180/


All Articles