提高:: ASIO :: async_read_until读取所有数据,而不只是一些 [英] boost::asio::async_read_until reads all data instead of just some

查看:2460
本文介绍了提高:: ASIO :: async_read_until读取所有数据,而不只是一些的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改升压短耳回声例如使用 async_read_until 按字读输入字。即使我使用 async_read_until ,所有发送的数据似乎从套接字读取。可能有人请告知:

 的#include< cstdlib>
#包括LT&;&iostream的GT;
#包括LT&;升压/ bind.hpp>
#包括LT&;升压/ asio.hpp>使用boost ::支持ASIO ::知识产权:: TCP;堂课
{
上市:
  会话(提高:: ASIO :: io_service对象和放大器; io_service对象)
    :socket_(io_service对象)
  {
  }  TCP ::插座及放大器;插座()
  {
    返回socket_;
  }  无效的start()
  {
 性病::法院LT&;<启动<<的std :: ENDL;
  提高:: ASIO :: async_read_until(socket_,缓冲,'',
        提高::绑定(安培;会议:: handle_read,为此,
          提高:: ASIO ::占位符::错误
          提高:: ASIO ::占位符:: bytes_transferred));
  }  无效handle_read(常量的boost ::系统::错误_ code和;错误,
      为size_t bytes_transferred)
  { 的std :: ostringstream SS;
 SS<<&安培;缓冲;
 性病::字符串s = ss.str();
 性病::法院LT&;< S<<的std :: ENDL; 如果(!错误)
    {
      提高:: ASIO :: async_write(socket_,
          提高:: ASIO ::缓​​冲液(S),
          提高::绑定(安培;会议:: handle_write,为此,
            提高:: ASIO ::占位符::错误));
    }
    其他
    {
      删除;
    }
  }  无效handle_write(常量的boost ::系统::错误_ code&安培;错误)
  {
 性病::法院LT&;<写处理<<的std :: ENDL;
    如果(!错误)
    {
    }
    其他
    {
      删除;
    }
  }私人的:
  TCP ::插座socket_;
  提高:: ASIO ::流缓冲缓冲;
};级服务器
{
上市:
  服务器(提高:: ASIO :: io_service对象和放大器; io_service对象,短端口)
    :io_service_(io_service对象)
      acceptor_(io_service对象,TCP ::端点(TCP :: V4(),口))
  {
    会议* new_session =新的会话(io_service_);
    acceptor_.async_accept(new_session->插座(),
        提高::绑定(安培;服务器:: handle_accept,为此,new_session,
          提高:: ASIO ::占位符::错误));
  }  无效handle_accept(会话* new_session,
      常量的boost ::系统::错误_ code&放;错误)
  {
    如果(!错误)
    {
      new_session->启动();
      new_session =新的会话(io_service_);
      acceptor_.async_accept(new_session->插座(),
          提高::绑定(安培;服务器:: handle_accept,为此,new_session,
            提高:: ASIO ::占位符::错误));
    }
    其他
    {
      删除new_session;
    }
  }私人的:
  提高:: ASIO :: io_service对象和放大器; io_service_;
  TCP ::受acceptor_;
};INT主(INT ARGC,CHAR *的argv [])
{
  尝试
  {
    如果(的argc!= 2)
    {
      的std :: CERR<< 用法:async_tcp_echo_server<港口> \\ N的;
      返回1;
    }    提高:: ASIO :: io_service对象io_service对象;    使用命名空间std; //为的atoi。
    服务器S(io_service对象,与atoi(ARGV [1]));    io_service.run();
  }
  赶上(性病::例外急症)
  {
    的std :: CERR<< 例外:<< e.what()&所述;&下; \\ n;
  }  返回0;
}


解决方案

阅读<一href=\"http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/reference/async_read_until/overload1.html\">description async_read_until的谨慎。它说:


  

成功async_read_until后
  操作中,流缓冲可以含有
  超出定界符附加数据


这意味着你的情况是,里面handle_read(),你应该只访问第一个 bytes_transferred 从缓冲区的字节。截至目前,你的 bytes_transferred 参数未使用。

I'm modify the Boost Asio echo example to use async_read_until to read the input word by word. Even though I am using async_read_until, all the data sent seems to be read from the socket. Could someone please advise:

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

class session
{
public:
  session(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
 std::cout<<"starting"<<std::endl;
  boost::asio::async_read_until(socket_, buffer, ' ',
        boost::bind(&session::handle_read, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

  void handle_read(const boost::system::error_code& error,
      size_t bytes_transferred)
  {

 std::ostringstream ss;
 ss<<&buffer;
 std::string s = ss.str();
 std::cout<<s<<std::endl;

 if (!error)
    {
      boost::asio::async_write(socket_,
          boost::asio::buffer(s),
          boost::bind(&session::handle_write, this,
            boost::asio::placeholders::error));
    }
    else
    {
      delete this;
    }
  }

  void handle_write(const boost::system::error_code& error)
  {
 std::cout<<"handling write"<<std::endl;
    if (!error)
    {
    }
    else
    {
      delete this;
    }
  }

private:
  tcp::socket socket_;
  boost::asio::streambuf buffer;
};

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
  {
    session* new_session = new session(io_service_);
    acceptor_.async_accept(new_session->socket(),
        boost::bind(&server::handle_accept, this, new_session,
          boost::asio::placeholders::error));
  }

  void handle_accept(session* new_session,
      const boost::system::error_code& error)
  {
    if (!error)
    {
      new_session->start();
      new_session = new session(io_service_);
      acceptor_.async_accept(new_session->socket(),
          boost::bind(&server::handle_accept, this, new_session,
            boost::asio::placeholders::error));
    }
    else
    {
      delete new_session;
    }
  }

private:
  boost::asio::io_service& io_service_;
  tcp::acceptor acceptor_;
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: async_tcp_echo_server <port>\n";
      return 1;
    }

    boost::asio::io_service io_service;

    using namespace std; // For atoi.
    server s(io_service, atoi(argv[1]));

    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

解决方案

Read the description of async_read_until carefully. It says:

After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter.

What this means in your case is that inside handle_read(), you should only access the first bytes_transferred bytes from the buffer. As of now, your bytes_transferred parameter is unused.

这篇关于提高:: ASIO :: async_read_until读取所有数据,而不只是一些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆