我尝试创建一个在服务器上保持连接活动的客户端.
但是,当我收到一次数据时,连接已关闭.我不明白为什么.
我认为我应该制作一个循环,但我们告诉我这不是一个好主意.
class client
{
public:
client(boost::asio::io_service& io_service,
const std::string& host, const std::string& service)
: connection_(io_service)
{
// Resolve the host name into an IP address.
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(host, service);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
resolver.resolve(query);
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
// Start an asynchronous connect operation.
connection_.socket().async_connect(endpoint,
boost::bind(&client::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
}
/// Handle completion of a connect operation.
void handle_connect(const boost::system::error_code& e,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
if (!e)
{
// Successfully established connection. Start operation to read the list
// …Run Code Online (Sandbox Code Playgroud)