久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ Boost ASIO:如何讀/寫超時(shí)?

C++ Boost ASIO: how to read/write with a timeout?(C++ Boost ASIO:如何讀/寫超時(shí)?)
本文介紹了C++ Boost ASIO:如何讀/寫超時(shí)?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

通過閱讀其他 Stack Overflow 條目和 boost::asio 文檔,我確認(rèn)沒有同步 ASIO 讀/寫調(diào)用也提供易于使用的超時(shí)作為調(diào)用的參數(shù).

From reading other Stack Overflow entries and the boost::asio documentation, I've confirmed that there is no synchronous ASIO read/write calls that also provide an easy-to-use timeout as a parameter to the call.

我正在使用使用超時(shí)的 select(2) 調(diào)用轉(zhuǎn)換老式 Linux 套接字應(yīng)用程序,我需要或多或少地做同樣的事情.

I'm in the middle of converting an old-school Linux socket application with select(2) calls that employs timeouts, and I need to do more-or-less the same.

那么在 boost::asio 中執(zhí)行此操作的最佳方法是什么?查看 asio 文檔,有許多與計(jì)時(shí)器有關(guān)的各種事情的令人困惑的示例,但我很困惑.

So what is the best way to do this in boost::asio? Looking at the asio documentation, there are many confusing examples of various things to do with timers, but I'm quite confused.

我很想看到一個(gè)簡(jiǎn)單易讀的例子:從套接字讀取,但最多等待 X 秒,之后函數(shù)要么不返回任何內(nèi)容,要么返回任何內(nèi)容能夠在超時(shí)到期之前從套接字讀取.

I'd love to see a simple-to-read example of this: Read from a socket, but wait for a maximum of X seconds after which the function either returns with nothing, or returns with whatever it was able to read from the socket before the timeout expired.

推薦答案

這已在 asio 郵件列表中提出,有一個(gè) ticket 也請(qǐng)求該功能.總而言之,如果您需要超時(shí)和可取消性,建議使用異步方法.

This has been brought up on the asio mailing lists, there's a ticket requesting the feature as well. To summarize, it is suggested to use asynchronous methods if you desire timeouts and cancellability.

如果您無法轉(zhuǎn)換為異步方法,您可以嘗試SO_RCVTIMEOSO_SNDTIMEO 套接字選項(xiàng).它們可以用 setsockopt 設(shè)置,描述符可以用 boost::asio::ip::tcp::socket::native 方法.man 7 socket 手冊(cè)頁說

If you cannot convert to asynchronous methods, you might try the SO_RCVTIMEO and SO_SNDTIMEO socket options. They can be set with setsockopt, the descriptor can be obtained with the boost::asio::ip::tcp::socket::native method. The man 7 socket man page says

SO_RCVTIMEO 和 SO_SNDTIMEO指定接收或發(fā)送超時(shí),直到報(bào)告錯(cuò)誤.參數(shù)是一個(gè)結(jié)構(gòu)體時(shí)間.如果輸入或輸出這個(gè)時(shí)期的功能塊時(shí)間,并且數(shù)據(jù)已經(jīng)發(fā)送或收到,該函數(shù)的返回值將是傳輸?shù)臄?shù)據(jù)量;如果不數(shù)據(jù)已傳輸并且已達(dá)到超時(shí)然后 -1 是返回 errno 設(shè)置為EAGAIN 或 EWOULDBLOCK 就像套接字被指定為是非阻塞的.如果超時(shí)是設(shè)置為零(默認(rèn))然后操作永遠(yuǎn)不會(huì)超時(shí).超時(shí)僅有效對(duì)于執(zhí)行套接字 I/O 的系統(tǒng)調(diào)用(例如,read(2),recvmsg(2), send(2), sendmsg(2));超時(shí)對(duì) select(2) 沒有影響,poll(2)、epoll_wait(2) 等

SO_RCVTIMEO and SO_SNDTIMEO Specify the receiving or sending timeouts until reporting an error. The argument is a struct timeval. If an input or output function blocks for this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been transferred and the timeout has been reached then -1 is returned with errno set to EAGAIN or EWOULDBLOCK just as if the socket was specified to be non-blocking. If the timeout is set to zero (the default) then the operation will never timeout. Timeouts only have effect for system calls that perform socket I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts have no effect for select(2), poll(2), epoll_wait(2), etc.

這篇關(guān)于C++ Boost ASIO:如何讀/寫超時(shí)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 五月婷婷导航 | 欧美中文字幕 | 国产一区二区三区在线看 | 国产日韩精品一区二区三区 | 国产午夜在线观看 | 国产精品久久久久久久久大全 | 毛色毛片免费看 | 99视频免费在线 | 亚洲精品精品 | 中文字幕在线剧情 | 亚洲综合第一页 | 日韩在线国产精品 | 久久精品在线播放 | 蜜桃在线一区二区三区 | av乱码 | 日本不卡一区二区三区 | 亚洲精品日韩一区二区电影 | 欧美日韩电影一区二区 | av中文天堂 | www.性色| 国产成人免费视频网站高清观看视频 | 亚洲精品无 | a级在线观看 | 91一区二区| 成人深夜福利 | 国产精品国产精品国产专区不片 | 少妇精品久久久久久久久久 | 亚洲一一在线 | 北条麻妃99精品青青久久主播 | 久久99这里只有精品 | 黄网站色大毛片 | 日日干夜夜操 | 夜夜精品视频 | h视频在线免费 | 久久久久亚洲精品 | 久久久国产一区二区三区四区小说 | 国产男女猛烈无遮掩视频免费网站 | 日韩网站在线观看 | 欧美三级视频 | 91婷婷韩国欧美一区二区 | 啪一啪在线视频 |