gpt4 book ai didi

c++ - Boost.Asio read_some : End of file error

转载 作者:搜寻专家 更新时间:2023-10-31 02:21:11 25 4
gpt4 key购买 nike

此示例基于书 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 block 中:

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

关于c++ - Boost.Asio read_some : End of file error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31738139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com