boost.asio中socket异步关闭流程

boost.asio中的socket异步方法,需要检测std::error_code来关闭连接,那如何正常关闭tcp连接呢。下面的模拟测试中:

1.server端在读取client端发送的第一个1024字节流后,关闭连接。
2.client端在另一个线程发送第一个1024字节流后,sleep1秒,再发送第二个1024字节流。

结果:
1.server端在关闭socket后,其异步方法会检测到错误。
2.client端的第二次发送也会检测到错误。
所以通过正常的错误处理,两端可以及时地关闭tcp连接。
可以得到如下结论:

  1. 一方close()后对方可通过write的回调函数发现错误,不必等到read时发现错误
  2. 多次地调用sock.close()不会出错
  3. sock.close()不能完全释放资源,需要使用delete sock
  4. delete sock会调用sock.close()
  5. sock.close()会调用sock.cancel()取消所有异步调用
  6. 在实际使用时主动write往往在另一个线程,这就需要加线程锁控制了

测试代码如下:

class LIBTEST_BOOST_DLL TcpServer
{
public:
	typedef  boost::asio::ip::tcp::socket tcp_socket_t;
	typedef  boost::asio::ip::tcp::acceptor tcp_acceptor_t;
	typedef  boost::asio::ip::tcp::endpoint tcp_endpoint_t;
	typedef  boost::asio::io_context io_context_t;
	TcpServer(io_context_t &io, const tcp_endpoint_t& endpoint);
	void Accept();
	void ReadHandler(tcp_socket_t *sock);
private:
	tcp_acceptor_t acceptor_;
	io_context_t *io_;
	uint64_t cnt_ = 0;
};
class LIBTEST_BOOST_DLL TcpClient
{
public:
	typedef  boost::asio::ip::tcp::socket tcp_socket_t;
	typedef  boost::asio::ip::tcp::endpoint tcp_endpoint_t;
	typedef  boost::asio::io_context io_context_t;
	TcpClient(io_context_t &io);
	void Connect(const tcp_endpoint_t& endpoint);
	void Write();
private:
	tcp_socket_t sock_;
	io_context_t  *io_;
	uint64_t cnt_ = 0;
};
TcpServer::TcpServer(io_context_t &io, const tcp_endpoint_t& endpoint)
	:acceptor_(io,endpoint),io_(&io)
{

}
void TcpServer::ReadHandler(tcp_socket_t *sock)
{
	char *buf = new char[1024];
	sock->async_read_some(boost::asio::buffer(buf,1024), [sock, this,buf](const std::error_code &ec, std::size_t sz) {
		delete buf;
		if (ec) {
		//call close again
			sock->close();
			cout << "[threadId=" << this_thread::get_id() << "] " <<  "async_read_some callback error,msg=" << ec.message()<< endl;
			delete sock;
			return;
		}
		//cout << "[" << this_thread::get_id() << "] " << &buf_[0];// << endl;
		cnt_++;
		cout << "[threadId=" << this_thread::get_id() << "] " << "async_read_some callback cnt=" << cnt_ << ",size=" << sz << endl;
		ReadHandler(sock);
		//call close many times after read 1024 bytes
		sock->close();
		sock->close();
		sock->close();
	});
}
void TcpServer::Accept()
{
	
	//auto sock = make_shared(*io_);
	auto sock = new tcp_socket_t(*io_);
	auto acceptHandler = [this,sock](const std::error_code &ec) {
		if (ec) {
			delete sock;
			cout << "[threadId=" << this_thread::get_id() << "] " << "async_accept callback error,msg=" << ec.message() << endl;
			return;
		}
		cout << "[threadId=" << this_thread::get_id() << "]";
		cout << " async_accept callback client from: ";
		cout << sock->remote_endpoint().address() <<":" << sock->remote_endpoint().port() << endl;
		ReadHandler(sock);
		Accept();
	};
	acceptor_.async_accept(*sock, acceptHandler);

}

TcpClient::TcpClient(io_context_t &io)
	:sock_(io),io_(&io)
{
}
void TcpClient::Write()
{
	auto buf = new char[1024];
	memset(buf,'q',1024);
	sock_.async_write_some(boost::asio::buffer(buf,1024), [this,buf](const std::error_code &ec, std::size_t sz) {
		delete buf;
		if (ec) {
			cout << "[threadId=" << this_thread::get_id() << "] " << "async_write_some callback error,msg=" << ec.message() << endl;
			sock_.close();
			return;
		}
		cnt_++;
		cout << "[threadId=" << this_thread::get_id() << "] " << "async_write_some callback cnt=" << cnt_ << ",size=" << sz<< endl;
	});
	
}
void TcpClient::Connect(const tcp_endpoint_t& endpoint)
{
	auto connHandler = [this,endpoint](const std::error_code &ec) {
		if (ec) {
			cout << "[threadId=" << this_thread::get_id() << "] " << "async_connect callback error,msg=" << ec.message() << endl;
			//Connect(endpoint);
			return;
		}
		cout << "[threadId=" << this_thread::get_id() << "] ";
		cout << "async_connect callback to ";
		cout << this->sock_.remote_endpoint().address();
		cout << " success" << endl;;
		std::thread t([this] {
			for(int i=0;i<1;i++)
			Write();
			this_thread::sleep_for(chrono::seconds(1));
			Write();
		});
		t.detach();
	};
	sock_.async_connect(endpoint, connHandler);
}
int main(int argc, char *argv[])
{

	boost::asio::io_context io;
	
	boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 1234);
	TcpServer tcp(io,endpoint);
	TcpClient client(io);
	tcp.Accept();
	client.Connect(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"),1234));
	boost::asio::io_context::work worker(io);
	io.run();

	
#ifdef WIN32
	system("pause");
#endif
	return 0;
}

你可能感兴趣的:(#,boost,c++,boost,socket,多线程)