如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:47:52  来源:igfitidea点击:

How to handle asynchronous socket receiving in C++?

c++socketsasynchronoustcp

提问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.

回答by Robert S. Barnes

Depending on what you're doing, non-blocking I/O with selectmay be the answer.

根据您在做什么,选择非阻塞 I/O可能是答案。

Take a look at this tutorial.

看看这个教程