Boost.Asio read_some: 文件结束错误

Boost.Asio read_some: End of file error

此示例基于 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

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

您的服务器希望客户端在连接后发送一些数据,但事实并非如此。连接后客户端直接退出

您的客户端的以下修改只是在无限循环中将数据发送到始终响应 "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