我的问题是当您不知道消息大小时,读取 boost tcp 套接字的最佳方法是什么?这是一个样本。我只是把大小设置为 10。代码在不同的标题中,但我觉得不需要全部包含在内
/////////////hpp
typedef boost::asio::ip::tcp::resolver TResolver;
typedef boost::asio::ip::tcp::resolver::query TResolverQuery;
typedef boost::asio::ip::tcp::endpoint Tendpoint;
typedef boost::asio::ip::tcp::socket Tsocket;
typedef boost::asio::ip::tcp::acceptor Tacceptor;
#define Tsize 10
////////////cpp
while(true)
{
cout<<"recieve"<<endl;
boost::shared_ptr< Tacceptor > acceptor(new boost::asio::ip::tcp::acceptor(*m_io_service));
boost::system::error_code ec;
TResolverQuery query(this->getHost(), boost::lexical_cast< std::string >( this->getPort() ));
TResolver resolver( *m_io_service );
Tendpoint endpoint = *resolver.resolve( query );
acceptor->open( endpoint.protocol() );
acceptor->set_option( boost::asio::ip::tcp::acceptor::reuse_address( true ) );
acceptor->bind( endpoint );
acceptor->listen();
boost::shared_ptr< Tsocket > socknew(new Tsocket( *m_io_service ));
acceptor->accept( *socknew,endpoint);
boost::array<char, Tsize> buf;
boost::asio::read(*socknew,boost::asio::buffer(buf), ec);
cout.write(buf.data(),Tsize);cout << std::endl;
socknew->shutdown( boost::asio::ip::tcp::socket::shutdown_both, ec );
cout<<"threadId "<< boost::this_thread::get_id()<<endl;
}