• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統計

            最新評論

            gethostbyname timeout --- 轉

            總結: 下面第一種方法經過測試是可行的,第二種方法(多線程中使用)不能夠用信號中斷。

            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.

            posted on 2012-11-12 14:45 大龍 閱讀(3291) 評論(0)  編輯 收藏 引用

            久久久久亚洲av成人网人人软件| 久久精品国产亚洲AV蜜臀色欲| 久久综合色老色| 激情综合色综合久久综合| 91精品国产9l久久久久| 香蕉久久夜色精品升级完成| 久久99这里只有精品国产| 久久丝袜精品中文字幕| 久久久这里有精品中文字幕| 国产日韩久久免费影院| 国产精品欧美久久久久无广告| 久久亚洲综合色一区二区三区| 免费观看成人久久网免费观看| 久久久久综合网久久| 老司机国内精品久久久久| 国产成人精品久久亚洲高清不卡 | 7国产欧美日韩综合天堂中文久久久久 | 精品欧美一区二区三区久久久| 色综合久久88色综合天天| 91久久精品国产91性色也| 免费一级欧美大片久久网| 久久只有这精品99| 嫩草伊人久久精品少妇AV| 精品国产一区二区三区久久| 国产精品久久久天天影视香蕉 | 国产精品久久久久免费a∨| 中文字幕精品无码久久久久久3D日动漫 | 久久亚洲国产欧洲精品一| 国内精品欧美久久精品| 久久影院亚洲一区| 久久综合给合久久狠狠狠97色69| 狠狠色丁香久久综合五月| 久久精品二区| 久久夜色精品国产噜噜麻豆| 国产精品成人无码久久久久久| 久久久久亚洲av综合波多野结衣| 久久久久久久亚洲Av无码| 国产成人久久精品麻豆一区| 亚洲AV无码久久精品狠狠爱浪潮| 久久久久久国产精品免费免费| 久久精品午夜一区二区福利|