c++ - Boost ASIO receive_from 如何返回底层套接字错误?

标签 c++ sockets boost-asio

即阻塞版本是否会在出错时返回 -1。

或者更重要的是,你怎么知道调用失败了?做

boost::asio::ip::udp::socket::receive_from() 错误时返回 -1

正常的 BSD 套接字 API receive_from 调用将在错误时返回 -1,您可以查看 errno 以了解具体的错误代码。增强版是否也这样做?

好的,我试过像这样传递重载:

while(true){
    boost::system::error_code ec;
    size_t length = socket_.receive_from(
        boost::asio::buffer(buffer, buffer_size), 
        sender_endpoint, ec);

    /// ... other code
}

但是我在编译时遇到这个错误:

error: no matching function for call to ‘boost::asio::basic_datagram_socket<boost::asio::ipdp, boost::asio::datagram_socket_service<boost::asio::ip::udp> >::receive_from(boost::asio::mutable_buffer, boost::asio::ip::basic_endpoint<boost::asio::ip::udp>&, boost::system::error_code&)’
make: *** [server.o] Error 1

是文档有误还是我做错了什么? 编辑:不,我错了……对不起,我有时间……这叫做过度工作!我现在半睡半醒。我缺少标志参数。

最佳答案

它抛出一个 boost::system::system_error失败时异常(exception)。有一个 overload如果您不想处理异常,则采用参数。

关于c++ - Boost ASIO receive_from 如何返回底层套接字错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3898547/

相关文章:

c++ - C++中虚函数的动态绑定(bind)

c++ - 在 Linux 中将 fork() + execlp 与 boost::asio 一起使用时解决重用错误

c++ - asio::io_service 工作立即结束

c++ - Asio UDP 套接字接收失败

c++ - 通过 Boost ASIO 服务器发送缓冲区 - 发送错误数据

unix 中的 c++ 编译器版本

C++ 空值和 this 指针

c++ - 退出进程时等待线程完成

c++ - 从 Qt 中的套接字中读取所有数据

c套接字: flushing data to let client know its okay to type