非阻塞网络地址解析(gethostbyname或getaddrinfo)? [英] non-blocking network address resolution (gethostbyname or getaddrinfo)?

查看:377
本文介绍了非阻塞网络地址解析(gethostbyname或getaddrinfo)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用经典的nsswitch.conf配置:

Using a classic nsswitch.conf configuration :

hosts:      files dns

如果DNS无法应答(或配置错误),尝试使用 gethostbyname (或 getaddrinfo )解析名称可能需要一段时间.

Trying to resolve a name using gethostbyname (or getaddrinfo) could take a while if the DNS does not answer (or is badly configured).

是否可以在nsswitch.conf中配置超时或使用替代API管理超时?

Is there a way to configure a timeout in nsswitch.conf or use an alternative api that manage a timeout ?

推荐答案

getaddrinfo_a ,但是特定于Linux 特定于GNU glibc.另外,您可以生成一个线程并在其中调用getaddrinfo.不要在线程中使用gethostbyname,因为它不是线程安全.

There is getaddrinfo_a, but it is Linux-specific GNU glibc specific. Alternatively, you can spawn a thread and call getaddrinfo in it. Don't use gethostbyname in a thread, as it is not thread-safe.

这篇关于非阻塞网络地址解析(gethostbyname或getaddrinfo)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆