Sockets boost async\u accept不使用boost asio使用\u future选项
我想在有超时的Sockets boost async\u accept不使用boost asio使用\u future选项,sockets,c++11,boost-asio,future,Sockets,C++11,Boost Asio,Future,我想在有超时的boost::asio::ip::tcp::socket上侦听。为此,我使用了std::future::wait_For函数。下面是我的代码: std::optional<boost::asio::ip::tcp::socket> server::listen() { boost::asio::ip::tcp::socket sock(io_service); std::future<void> accept_status = accepto
boost::asio::ip::tcp::socket上侦听。为此,我使用了std::future::wait_For
函数。下面是我的代码:
std::optional<boost::asio::ip::tcp::socket> server::listen()
{
boost::asio::ip::tcp::socket sock(io_service);
std::future<void> accept_status = acceptor.async_accept(
sock, boost::asio::use_future);
if (accept_status.wait_for(std::chrono::seconds(10)) == std::future_status::timeout)
{
// I hope there's no race-condition between
// accepting a connection and calling cancel
acceptor.cancel();
std::cerr << "Timeout" << std::endl;
return {};
}
std::cerr << "Accepted a connection" << std::endl;
return {std::move(sock)};
}
由g++-std=c++17.cpp-lpthread-lboost\u system-o编译
我得到的结果是:
Connected to server
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
Timeout
...
要回答您的要求:
“future对象和异步接受函数不通信”--不可能
“客户端可以连接,但我仍然得到一个超时。”,--您的客户端连接到侦听器是一个事件,执行完成处理程序(设置承诺)是另一个事件
所以连接可能在第9秒被接受,而回调可能计划在第11秒运行(例如)
记住,我们处理的是异步操作,所以我认为对未来事件进行绝对预测是不对的
除此之外
// I hope there's no race-condition between
// accepting a connection and calling cancel
acceptor.cancel();
std::cerr << "Timeout" << std::endl;
return {};
在您的程序中有许多事情需要注意(避免不必要的旋转循环,不要忘记连接或分离std::thread
,并确保在使用async*
version时调用io\u service::run
)
你增加了超时时间并检查了吗?嗨@Explorer\N,我已经编辑了这个问题以添加一个示例程序。你能在你的系统上测试它吗?你有没有看到io\u service.run()
?谢谢。我应该研究如何在异步调用中正确使用boost io服务。
// I hope there's no race-condition between
// accepting a connection and calling cancel
acceptor.cancel();
std::cerr << "Timeout" << std::endl;
return {};
using namespace std;
void server_listen() {
boost::asio::io_service io_service;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 31132);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
boost::asio::ip::tcp::socket socket(io_service);
auto work = make_work_guard(io_service);
using type= std::decay_t<decltype(work)>;
std::thread io([&](){io_service.run();});
std::future<void> accept_status = acceptor.async_accept(
socket, boost::asio::use_future);
if(accept_status.wait_for(std::chrono::seconds(10)) == std::future_status::timeout) {
acceptor.cancel();
std::cerr << "Timeout\n";
work.~type();
//break;
} else {
std::cout<<"future is ready\n";
work.~type();
// break;
}
io.join();
// if I replace the lines starting from the async_accept call
// by just the following, everything works as expected
// acceptor.accept(socket);
std::cout << "Accepted a connection\n";
}
void client_connect() {
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::socket socket(io_service);
boost::asio::ip::tcp::endpoint endpoint(*resolver.resolve({"127.0.0.1", std::to_string(31132)}));
socket.connect(endpoint);
std::cout << "Connected to server\n";
}
enter code here
int main() {
std::thread server(server_listen);
std::this_thread::sleep_for(std::chrono::seconds(2));
std::thread client(client_connect);
server.join(); client.join();
}
Start
Connected to server
future is ready
Accepted a connection
0
Finish