简体   繁体   English

C ++:boost :: asio:write()不支持UDP套接字吗?

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

Trying to use boost::asio::write() to write to a boost::asio::ip::udp::socket object. 尝试使用boost::asio::write()写入boost::asio::ip::udp::socket对象。 While I'm busy re-reading the docs to see if I've gone wrong somewhere, can someone confirm if maybe this is not supported? 虽然我正在忙着重新阅读文档以查看我是否在某个地方出错,但是有人可以确认是否可能不支持此操作吗? I'm now thinking boost::asio::write() only supports tcp::socket objects, not udp::socket . 我现在想的是boost::asio::write()只支持tcp::socket对象,而不支持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() when I read this in the docs: boost::asio::write()我在文档中读到这个时,我决定尝试使用boost::asio::write()

The send operation may not transmit all of the data to the peer. 发送操作可能不会将所有数据发送到对等方。 Consider using the write function if you need to ensure that all data is written before the blocking operation completes.` 如果需要确保在阻塞操作完成之前写入所有数据,请考虑使用write函数

...but in going back, I see that text is only in boost::asio::ip::tcp::socket::send() and not in the UDP version. ...但是回过头来看,我看到文本只在boost::asio::ip::tcp::socket::send()而不是在UDP版本中。 Source. 资源。

Assuming you want synchronous behavior, you need to use the send or send_to member methods of the boost::asio::ip::udp::socket class. 假设您需要同步行为,则需要使用boost::asio::ip::udp::socket类的sendsend_to成员方法。 The boost::asio::write free function uses the SyncWriteStream type requirement, which a UDP socket does not meet. boost::asio::write free函数使用SyncWriteStream类型要求,UDP套接字不符合要求。

I use async_send_to instead. 我使用async_send_to代替。 Perhaps that's possible for you too? 也许你也有可能吗?

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_socket is an asio::ip::udp::socket . 其中m_socketasio::ip::udp::socket

Seems to me that asio::write requires the stream class to have write_some implemented. 在我看来,asio :: write要求流类实现write_some。 IIRC, When writing to UDP sockets you want to write everything at one time in order to reduce fragmentation (unless your data + UDP header + ip header size is > PMTU, then you've got some work ahead of you). IIRC,当写入UDP套接字时,您希望一次写入所有内容以减少碎片(除非您的数据+ UDP标头+ ip标头大小> PMTU,那么您还有一些工作要做)。

Do you have code on the receiving side that reorders/reassembles the UDP packets? 您是否在接收方有重新排序/重新组装UDP数据包的代码? You will get UDP packets out of order and fragmented no matter how good your network is. 无论你的网络有多好,你都会得到无序的UDP数据包和碎片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM