您现在的位置是:首页 > 电脑 > 

检查basic

2025-07-28 10:15:15
检查basic_socket_acceptor是否准备好接受(Check if basic_socket_acceptor is ready to accept) 我正在使用etworking TS编写一个简单的单线程TCP服务器。 为了接受传入连接,我创建了std::experimental::net::ip::tcp::acceptor并调用accept成
检查basic_socket_acceptor是否准备好接受(Check if basic_socket_acceptor is ready to accept)

我正在使用etworking TS编写一个简单的单线程TCP服务器。 为了接受传入连接,我创建了std::experimental::net::ip::tcp::acceptor并调用accept成员函数。 但是如果没有连接可以接受,我就不会这么做。 如何检查接受器是否已准备好接受?

I'm writing a simple single threaded TCP server using etworking TS. In order to accept incoming connecti, I create std::experimental::net::ip::tcp::acceptor and call accept member function. But it blocks if there is no connection to accept and I don't want that. How can I check if acceptor is ready to accept?

最满意答案

显然,如果有一个函数最终会调用select ,它会暴露竞争条件 。 正确的方法是将acceptor设置为非阻塞。

std::experimental::net::io_context context; std::experimental::net::ip::tcp::acceptor acceptor{context, std::experimental::net::ip::tcp::endpoint{ std::experimental::net::ip::tcp::v4(), 124}}; _blocking(true); std::error_code error; while (true) { auto socket = acceptor.accept(error); if (!error) { // Handle incoming connection } // Do other stuff }

Apparently, if there would be a function that would ultimately call select, it would expose a race condition. The correct way is to set acceptor to be non-blocking.

std::experimental::net::io_context context; std::experimental::net::ip::tcp::acceptor acceptor{context, std::experimental::net::ip::tcp::endpoint{ std::experimental::net::ip::tcp::v4(), 124}}; _blocking(true); std::error_code error; while (true) { auto socket = acceptor.accept(error); if (!error) { // Handle incoming connection } // Do other stuff }

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697397.html

相关标签:无
上传时间: 2023-08-27 09:53:40
留言与评论(共有 6 条评论)
本站网友 中华通
21分钟前 发表
experimental
本站网友 沧州市实验小学
26分钟前 发表
acceptor and call accept member function. But it blocks if there is no connection to accept and I don't want that. How can I check if acceptor is ready to accept? 最满意答案 显然
本站网友 牛皮癣的治疗
5分钟前 发表
std
本站网友 证券业从业资格考试
28分钟前 发表
ip
本站网友 北京阜外医院官网
21分钟前 发表
tcp