当 `boost::asio::ip::tcp::resolver::resolve()` 失败时,应该提供哪个 `boost::system::error_code` 值?
我想返回一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须提供错误代码和类别才能创建
error_code
对象。下面是一个示例,假设错误是由于另一台主机拒绝连接造成的:使用系统类别时,您还可以传递 errno 值作为错误代码。例如:
不幸的是,这个库的记录很差,所以我建议您查看 头文件把事情弄清楚。该代码相当简单且直接。
以防万一您的编译器支持 C++11 并且您愿意使用它,此功能已使其成为标准。据我所知 gcc 4.6.1 已经有了。这是一个简单的例子:
通常,如果不需要抛出,库会传递
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:You can also pass
errno
value as error code when using system category. For example: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:
Generally, libraries pass
error_code
object around if there is no need to throw and usesystem_error
to throw an exception describing system failures. Another reason to useerror_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!
从外部
resolve()
不可能得到正确的结果。但是您可以让它为您执行此操作,方法是使用将error_code&
作为输出参数的重载之一:迭代器解析(const query & q, boost::system::error_code & ec)
迭代器解析(const endpoint_type & e, boost::system::error_code & ec)
然后返回它设置的error_code。我相信这将结束
errno
或h_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 anerror_code&
as an out-parameter:iterator resolve(const query & q, boost::system::error_code & ec)
iterator resolve(const endpoint_type & e, boost::system::error_code & ec)
and then return the error_code it sets. I trust that this will wrap up
errno
orh_errno
as appropriate.