在从客户端接收数据时的异常 - “文件结束”错误 [英] Exception while receiving data from client - "End of file" error

查看:157
本文介绍了在从客户端接收数据时的异常 - “文件结束”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Boost ASIO处理客户端服务器TCP / IP应用程序。
客户端使用以下语句将数据发送到服务器

I am currently working on a client server TCP/IP application using Boost ASIO. The client sends data to the server using the following statement

boost::system::error_code ignored_error; 
boost::asio::write(socket, boost::asio::buffer("Hello World\0"), ignored_error);

服务器读取这些数据(从Asynch。读取)

The server reads this data as such (from an Asynch. read)

boost::asio::async_read(*sock, boost::asio::buffer(buf_server),boost::bind(&ServerRead,boost::asio::placeholders::error));

这里是阅读的地方

boost::array<char, 158> buf_server;

void ServerRead(const boost::system::error_code& error)
{
    if(!error)
    {
        std::cout << "Message: " << buf_server.data() << std::endl;
    }
    else
    {
        std::cout << "Error occurred." << error.message() << std::endl;
    }
}

但是,我得到错误End of file。
现在我有两个问题

However I get the error "End of file". So now I have two questions


  1. 如何解决这个问题。

  1. How can I resolve this issue. What am i doing wrong here ?

假设我的 buf_server 只有5个字符,而不是158个。我可以让我的接收器使用这个缓冲区只读取整个数据和 std :: cout it?

Suppose my buf_server was only 5 characters instead of 158. How could I make my receiver use this buffer only to read the entire data and std::cout it?


推荐答案

当指示应用程序读取158个字节,但只发送12个字节时,应该会出现此行为。要解决此问题,您可以发送一个固定大小的标题,指示邮件大小,聊天客户端和服务器示例包含在Asio文档中,显示了如何完成此操作,从客户端示例中摘录:

This behavior should be expected when you instruct an application to read 158 bytes but only 12 are sent. To resolve this you can send a fixed size header indicating the message size, the chat client and server example included in the Asio documentation shows how to accomplish this, an excerpt from the client example:

void handle_connect(const boost::system::error_code& error)
  {
    if (!error)
    {
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.data(), chat_message::header_length),
          boost::bind(&chat_client::handle_read_header, this,
            boost::asio::placeholders::error));
    }
  }

  void handle_read_header(const boost::system::error_code& error)
  {
    if (!error && read_msg_.decode_header())
    {
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
          boost::bind(&chat_client::handle_read_body, this,
            boost::asio::placeholders::error));
    }
    else
    {
      do_close();
    }
  }

  void handle_read_body(const boost::system::error_code& error)
  {
    if (!error)
    {
      std::cout.write(read_msg_.body(), read_msg_.body_length());
      std::cout << "\n";
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.data(), chat_message::header_length),
          boost::bind(&chat_client::handle_read_header, this,
            boost::asio::placeholders::error));
    }
    else
    {
      do_close();
    }
  }

或者使用更奇特的协议如HTTP 直到遇到一个 \r\\\
分隔符。

Alternatively, or use a more exotic protocol such as HTTP where you read until encountering a \r\n delimiter.

这篇关于在从客户端接收数据时的异常 - “文件结束”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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