当 `boost::asio::ip::tcp::resolver::resolve()` 失败时,应该提供哪个 `boost::system::error_code` 值?

发布于 2024-12-07 17:09:42 字数 115 浏览 1 评论 0原文

我想返回一个 boost::system::error_code 指示主机/服务是否可以解析。主机/服务查找失败可能有多种原因(例如网络连接问题或无效参数)。

应该退回什么?

I want to return a boost::system::error_code indicationg whether a host/service could be resolved or not. There might be multiple reasons why a host/service look-up failed (e.g. network connection problems or an invalid argument).

What should be returned?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任性一次 2024-12-14 17:09:42

您必须提供错误代码和类别才能创建 error_code 对象。下面是一个示例,假设错误是由于另一台主机拒绝连接造成的:

error_code ec (errc::connection_refused, system_category());
return ec;

使用系统类别时,您还可以传递 errno 值作为错误代码。例如:

#include <fstream>
#include <cerrno>
#include <boost/system/system_error.hpp>

void foo ()
{
    ifstream file ("test.txt");
    if (!file.is_open ())
    {
        int err_code = errno;
        boost::system::error_code ec (err_code
            , boost::system::system_category ());
        throw boost::system::system_error (ec, "cannot open file");
    }
}

不幸的是,这个库的记录很差,所以我建议您查看 头文件把事情弄清楚。该代码相当简单且直接。

以防万一您的编译器支持 C++11 并且您愿意使用它,此功能已使其成为标准。据我所知 gcc 4.6.1 已经有了。这是一个简单的例子:

#include <cerrno>
#include <system_error>

std::error_code
SystemError::getLastError ()
{
    int err_code = errno;
    return std::error_code (err_code, std::system_category ());
}

void foo ()
{
    throw std::system_error (getLastError (), "something went wrong");
}

通常,如果不需要抛出,库会传递 error_code 对象,并使用 system_error 抛出描述系统故障的异常。无异常地使用 error_code 的另一个原因是当您需要跨不同线程发出错误信号时。但 C++11 有跨线程传播异常的解决方案

希望有帮助!

You have to come up with error code and category in order to create error_code object. Here is an example, assuming that error is due to another host refusing connection:

error_code ec (errc::connection_refused, system_category());
return ec;

You can also pass errno value as error code when using system category. For example:

#include <fstream>
#include <cerrno>
#include <boost/system/system_error.hpp>

void foo ()
{
    ifstream file ("test.txt");
    if (!file.is_open ())
    {
        int err_code = errno;
        boost::system::error_code ec (err_code
            , boost::system::system_category ());
        throw boost::system::system_error (ec, "cannot open file");
    }
}

Unfortunately, this library is poorly documented, so I can recommend you to look into header files to figure things out. The code is fairly simple and straight forward there.

Just in case your compiler supports C++11 and you are willing to use it, this functionality made it into standard. As far as I know gcc 4.6.1 has it already. Here is a simple example:

#include <cerrno>
#include <system_error>

std::error_code
SystemError::getLastError ()
{
    int err_code = errno;
    return std::error_code (err_code, std::system_category ());
}

void foo ()
{
    throw std::system_error (getLastError (), "something went wrong");
}

Generally, libraries pass error_code object around if there is no need to throw and use system_error to throw an exception describing system failures. Another reason to use error_code without exceptions is when you need to signal the error across different threads. But C++11 has a solution for propagating exceptions across threads.

Hope it helps!

神也荒唐 2024-12-14 17:09:42

从外部 resolve() 不可能得到正确的结果。但是您可以让它为您执行此操作,方法是使用将 error_code& 作为输出参数的重载之一:

然后返回它设置的error_code。我相信这将结束 errnoh_errno(视情况而定)。

It's impossible to get this right from outside resolve(). But you can get it to do it for you, by using one of the overloads that takes an error_code& as an out-parameter:

and then return the error_code it sets. I trust that this will wrap up errno or h_errno as appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文