boostorg / boostorg/boost

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

Open
#832 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
8.6k
Forks
1.9k
Avg merge
39m
Merged PRs (30d)
2

Description

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;
}

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.