在从客户端接收数据时的异常

编程入门 行业动态 更新时间:2024-10-26 08:28:55
本文介绍了在从客户端接收数据时的异常 - “文件结束”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用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

  • 如何解决这个问题。

  • 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.

  • 更多推荐

    在从客户端接收数据时的异常

    本文发布于:2023-10-26 19:17:27,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1531102.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:客户端   异常   数据

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!