boostorg / boost

Super-project for modularized Boost
https://github.com/boostorg/wiki/wiki/Getting-Started%3A-Overview
Boost Software License 1.0
7.13k stars 1.75k forks source link

The connection cannot be completed in Linux boost::asio::spawn #832

Open codinghahaha opened 11 months ago

codinghahaha commented 11 months ago

If the server starts after the connection cannot be reached.why? boost_1_73_0、Linux operating system

include

include "boost/asio.hpp"

include "boost/asio/spawn.hpp"

int main() { boost::asio::io_context ctx; auto ep = boost::asio::ip::tcp::endpoint(boost::asio::ip::make_address("127.0.0.1"), 1560); bool isConnect = false; bool isRun = false; boost::asio::ip::tcp::socket socket(ctx); while (true) { if (!isConnect && !isRun) { boost::asio::spawn([&](boost::asio::yield_context y){ isRun = true; boost::system::error_code ec; socket.async_connect(ep, y[ec]); if (ec) { isRun = false; socket.close(); printf("close\n"); } else { isConnect = true; printf("connect\n"); } }); } ctx.poll_one(); Sleep(1000); } return 0; }