如何在 C++ 中处理异步套接字接收?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3017234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle asynchronous socket receiving in C++?
提问by Overv
I'm currently using a thread to handle Connect and Send calls asynchronously. This is all working fine, but now I want to make receiving asynchronous too. How should I receive data without pausing the whole queue while waiting for data? The only solution I can think of right now is a second thread.
我目前正在使用一个线程来异步处理 Connect 和 Send 调用。这一切正常,但现在我也想让接收异步。我应该如何在等待数据时不暂停整个队列的情况下接收数据?我现在能想到的唯一解决方案是第二个线程。
回答by Nikolai Fetissov
Look into non-blocking socketsand polling APIs like select(2)
/poll(2)
/epoll(4)
/kqueue(2)
.
查找到非阻塞套接字和轮询API,如select(2)
/ poll(2)
/ epoll(4)
/ kqueue(2)
。
Specifically in C++, look into boost::asio
.
特别是在 C++ 中,查看boost::asio
.