gethostbyname timeout

gethostbyname timeout,第1张

概述总结: 下面第一种方法经过测试是可行的,第二种方法(多线程中使用)不能够用信号中断。 so , 对信号的使用要非常小心。 gethostbyname 是阻塞的, 非线程安全的,所以最好使用getaddrinfo , 它是线程安全的。 要给gethostbyname 设置一个超时值, 用信号(alarm)实现是不行的(目前项目嵌入式平台上面测试不行) 。 下面是网上摘录的解决该问题的方法, 对怎么绕

总结: 下面第一种方法经过测试是可行的,第二种方法(多线程中使用)不能够用信号中断。

so , 对信号的使用要非常小心。



gethostbyname 是阻塞的, 非线程安全的,所以最好使用getaddrinfo,它是线程安全的。

要给gethostbyname 设置一个超时值, 用信号(alarm)实现是不行的(目前项目嵌入式平台上面测试不行) 。


下面是网上摘录的解决该问题的方法, 对怎么绕过其他系统函数(无论是线程安全还是非安全的)也有很好的借鉴意义


Hello,I need to add timeout to gethostbyname function in my program. I Googled and searched archives,found some examples with alert(),but can't make it work :/ I would be grateful of someone Could tell me how to do it.
You can't. Or rather,you shouldn't. The trick with alarm(2) (not alert()),is to longjmp(3) from the signal handler to a context created with setjmp(3) before calling gethostbyname(3). This is,unfortunately,very common in Perl code (in Perl dIE() actually uses longjmp() internally to a context set from an eval statement). However,gethostbyname() keeps internal state,and afterward its unsafe to call the function again. gethostbyname_r() might be relatively safer,but you've probably also leaked a file descriptor and memory,meaning you Could only do it so many times from a single process before it won't work anymore. (And from a strict C perspective,jumPing from the signal handler itself is questionable.) You do have options. 1) Multiple processes. Use child processes and the gethostbyname()+alarm() trick,and return the answer down a pipe to the parent. The child will just kill itself if it timeouts (or alternatively the parent will set the alarm and kill the child),since it cannot reliably do it's job afterward. 2) Use threads in conjunction with gethostbyname_r(),or preferably getaddrinfo(3). Still,you cannot interrupt these functions within the thread,and so you have a few more decisions to make in terms of how you handle timeouts. 3) Use a third-party asynchronous DNS library: ADNS,C-Ares and UDNS are the first ones which comes to mind,the former two probably being the most popular. 总结

以上是内存溢出为你收集整理的gethostbyname timeout全部内容,希望文章能够帮你解决gethostbyname timeout所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1287322.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论
请登录后评论...
登录
后才能评论 提交

评论列表(0条)
    保存
    {label}