• <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>

            大龍的博客

            常用鏈接

            統(tǒng)計

            最新評論

            gethostbyname timeout --- 轉(zhuǎn)

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

            so , 對信號的使用要非常小心。



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

            要給gethostbyname 設(shè)置一個超時值, 用信號(alarm)實現(xiàn)是不行的(目前項目嵌入式平臺上面測試不行) 。


            下面是網(wǎng)上摘錄的解決該問題的方法, 對怎么繞過其他系統(tǒng)函數(shù)(無論是線程安全還是非安全的)也有很好的借鑒意義


            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)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            狠狠狠色丁香婷婷综合久久俺| 99久久99久久精品国产| 亚洲精品NV久久久久久久久久| 久久婷婷五月综合色99啪ak| 一本久久a久久精品综合香蕉 | 日韩精品久久久久久| 久久久久免费精品国产| 看全色黄大色大片免费久久久| 久久综合九色综合网站| 国产精品美女久久久| 久久夜色精品国产亚洲av| 亚洲国产精品综合久久网络| 久久精品中文无码资源站| 99久久精品九九亚洲精品| 精品综合久久久久久97| 久久99久久成人免费播放| 久久午夜伦鲁片免费无码| 天天做夜夜做久久做狠狠| 国产精品久久亚洲不卡动漫| 久久久国产打桩机| 日韩久久无码免费毛片软件| 精品综合久久久久久888蜜芽| 无码乱码观看精品久久| 蜜桃麻豆www久久| 99久久无色码中文字幕| 91麻豆国产精品91久久久| 久久久91人妻无码精品蜜桃HD| 国内精品久久久人妻中文字幕| 久久午夜夜伦鲁鲁片免费无码影视 | 久久亚洲国产精品一区二区| 77777亚洲午夜久久多喷| 亚洲国产天堂久久综合| 久久婷婷五月综合97色直播| 91精品国产91热久久久久福利| 久久久无码精品亚洲日韩蜜臀浪潮| 一级a性色生活片久久无少妇一级婬片免费放| 久久亚洲国产午夜精品理论片 | 麻豆精品久久久久久久99蜜桃 | 国产精品99久久99久久久| 新狼窝色AV性久久久久久| 亚洲中文字幕久久精品无码APP|