Boost.Asio read_some:文件结束错误

时间:2015-07-31 04:33:44

标签: c++ boost-asio

此示例基于书籍Boost.Asio C++ Network Programming一书中的第1章。 我正在尝试使用Boost.Asio库构建一个简单的客户端和同步服务器。

以下是客户端代码

#define BOOST_ASIO_SEPARATE_COMPILATION
#include <iostream>
#include <boost/asio/impl/src.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/write.hpp>
#include <string>

int main(int argc, char **argv) {
    boost::asio::io_service ioservice;
    boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"),2001);
    boost::asio::ip::tcp::socket sock(ioservice);
    sock.connect(ep);

    return 0;
}

以下是服务器代码。

#define BOOST_ASIO_SEPARATE_COMPILATION

#include <stdio.h>
#include <iostream>
#include <boost/asio/impl/src.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio/write.hpp>

using namespace boost::asio;
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> socket_ptr;
void client_session(socket_ptr sock);

int main(int argc, char **argv) {

    io_service ioservice;

    ip::tcp::endpoint ep(ip::tcp::v4(),2001);
    ip::tcp::acceptor acc(ioservice,ep);

    while(true){
      socket_ptr sock(new ip::tcp::socket(ioservice));
      acc.accept(*sock);
      boost::thread(boost::bind(client_session,sock));
    }

    return 0;
}

void client_session(socket_ptr sock)
{
   std::cout << "Handling client session \n";

    while(true){
      char data[512];
      size_t len = sock->read_some(buffer(data));
      if (len > 0)
    boost::asio::write(*sock,buffer("ok",2));

    }
}

运行服务器后,当我启动客户端时,我在服务器上得到以下输出

Handling client session
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
      what():  read_some: End of file
    Aborted

我不确定这里发生了什么。请帮忙。

1 个答案:

答案 0 :(得分:2)

您的服务器希望客户端在连接后发送一些数据,而不是。客户端只需在连接后退出。

客户端的以下修改只是将数据以无限循环方式发送到服务器,该服务器始终以“ok”响应。

#define BOOST_ASIO_SEPARATE_COMPILATION
#include <iostream>
#include <boost/asio/impl/src.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/write.hpp>
#include <string>

int main(int argc, char **argv) {
    boost::asio::io_service ioservice;
    boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"),2001);
    boost::asio::ip::tcp::socket sock(ioservice);
    sock.connect(ep);

    for(;;)
    {
      boost::array<char, 512> buf  = {{ 0 }};
      boost::system::error_code ignored_error;
      boost::asio::write(sock, boost::asio::buffer(buf), ignored_error);
      size_t len = sock.read_some(boost::asio::buffer(buf), ignored_error);
      std::cout.write(buf.data(), len);
    }

    return 0;
}

为了避免在客户端断开连接时崩溃服务器,您应该将read_some调用包装在try...catch块中:

void client_session(socket_ptr sock)
{
  std::cout << "Handling client session \n";

  try
  {
    while(true){
      char data[512];
      size_t len = sock->read_some(buffer(data));
      if (len > 0)
      boost::asio::write(*sock,buffer("ok",2));

    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }
}

您还应该查看boost asio文档中的示例:http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/tutorial.html