我如何使用带有共享指针的 Boost Asio 来解决这个 "Network connection aborted by local system"?
How can I get around this "Network connection aborted by local system" using Boost Asio with a shared pointer?
我正在尝试使用来自 Boost 1.60 的 Asio 使用几个不同的示例编写 TCP 客户端。连接正常工作大约 30 秒左右,但因错误而断开连接:
The network connection was aborted by the local system
我已尝试设置 "ping/pong" 设置以保持连接有效,但它仍然终止。我发现的唯一一个 Stack Overflow 答案建议使用 Boost 的 shared_from_this 和一个共享指针,我已经修改了我的代码以供使用。但是问题依旧。
设置连接对象及其线程:
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver res(ios);
boost::shared_ptr<Connection> conn = boost::shared_ptr<Connection>(new Connection(ios));
conn->Start(res.resolve(boost::asio::ip::tcp::resolver::query("myserver", "10635")));
boost::thread t(boost::bind(&boost::asio::io_service::run, &ios));
这里是连接 class 的相关部分(我确保在其他地方也使用 shared_from_this()):
class Connection : public boost::enable_shared_from_this<Connection>
{
public:
Connection(boost::asio::io_service &io_service)
: stopped_(false),
socket_(io_service),
deadline_(io_service),
heartbeat_timer_(io_service)
{
}
void Start(tcp::resolver::iterator endpoint_iter)
{
start_connect(endpoint_iter);
deadline_.async_wait(boost::bind(&Connection::check_deadline, shared_from_this()));
}
private:
void start_read()
{
deadline_.expires_from_now(boost::posix_time::seconds(30));
boost::asio::async_read_until(socket_, input_buffer_, 0x1f,
boost::bind(&Connection::handle_read, shared_from_this(), _1));
}
void handle_read(const boost::system::error_code& ec)
{
if (stopped_)
return;
if (!ec)
{
std::string line;
std::istream is(&input_buffer_);
std::getline(is, line);
if (!line.empty())
{
std::cout << "Received: " << line << "\n";
}
start_read();
}
else
{
// THIS IS WHERE THE ERROR IS LOGGED
std::cout << "Error on receive: " << ec.message() << "\n";
Stop();
}
}
void check_deadline()
{
if (stopped_)
return;
if (deadline_.expires_at() <= deadline_timer::traits_type::now())
{
socket_.close();
deadline_.expires_at(boost::posix_time::pos_infin);
}
deadline_.async_wait(boost::bind(&Connection::check_deadline, shared_from_this()));
}
};
原来问题出在服务器端。服务器未正确发送 "pong" 对客户端 ping 的响应,因此 async_read_until() 调用从未完成,因此从未重置截止时间计时器。
我正在尝试使用来自 Boost 1.60 的 Asio 使用几个不同的示例编写 TCP 客户端。连接正常工作大约 30 秒左右,但因错误而断开连接:
The network connection was aborted by the local system
我已尝试设置 "ping/pong" 设置以保持连接有效,但它仍然终止。我发现的唯一一个 Stack Overflow 答案建议使用 Boost 的 shared_from_this 和一个共享指针,我已经修改了我的代码以供使用。但是问题依旧。
设置连接对象及其线程:
boost::asio::io_service ios;
boost::asio::ip::tcp::resolver res(ios);
boost::shared_ptr<Connection> conn = boost::shared_ptr<Connection>(new Connection(ios));
conn->Start(res.resolve(boost::asio::ip::tcp::resolver::query("myserver", "10635")));
boost::thread t(boost::bind(&boost::asio::io_service::run, &ios));
这里是连接 class 的相关部分(我确保在其他地方也使用 shared_from_this()):
class Connection : public boost::enable_shared_from_this<Connection>
{
public:
Connection(boost::asio::io_service &io_service)
: stopped_(false),
socket_(io_service),
deadline_(io_service),
heartbeat_timer_(io_service)
{
}
void Start(tcp::resolver::iterator endpoint_iter)
{
start_connect(endpoint_iter);
deadline_.async_wait(boost::bind(&Connection::check_deadline, shared_from_this()));
}
private:
void start_read()
{
deadline_.expires_from_now(boost::posix_time::seconds(30));
boost::asio::async_read_until(socket_, input_buffer_, 0x1f,
boost::bind(&Connection::handle_read, shared_from_this(), _1));
}
void handle_read(const boost::system::error_code& ec)
{
if (stopped_)
return;
if (!ec)
{
std::string line;
std::istream is(&input_buffer_);
std::getline(is, line);
if (!line.empty())
{
std::cout << "Received: " << line << "\n";
}
start_read();
}
else
{
// THIS IS WHERE THE ERROR IS LOGGED
std::cout << "Error on receive: " << ec.message() << "\n";
Stop();
}
}
void check_deadline()
{
if (stopped_)
return;
if (deadline_.expires_at() <= deadline_timer::traits_type::now())
{
socket_.close();
deadline_.expires_at(boost::posix_time::pos_infin);
}
deadline_.async_wait(boost::bind(&Connection::check_deadline, shared_from_this()));
}
};
原来问题出在服务器端。服务器未正确发送 "pong" 对客户端 ping 的响应,因此 async_read_until() 调用从未完成,因此从未重置截止时间计时器。