gpt4 book ai didi

c++ - BOOST-ASIO 阻塞服务器不响应阻塞客户端?

转载 作者:可可西里 更新时间:2023-11-01 02:47:14 24 4
gpt4 key购买 nike

我最近有这个question解决了有问题的服务器,但事实证明服务器似乎没有回复客户端,也没有收到任何发送的数据。这就是我在客户端所做的。

C:\blocking_tcp_echo_client\bin\Debug>blocking_tcp_echo_client 127.0.0.1 4001
Enter message: hello
Exception: read: End of file

这是服务器端。我确实在启动客户端之前启动了服务器。我还用原来的 code 测试了客户端并且效果很好。

C:\boost_server_class\bin\Debug>boost_server_class
TCPIP_server:
Usage: blocking_tcp_echo_server

blocking_tcp_echo_client.cpp

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

int main(int argc, char* argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
return 1;
}

boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
tcp::resolver::iterator iterator = resolver.resolve(query);

tcp::socket s(io_service);
s.connect(*iterator);

using namespace std; // For strlen.
std::cout << "Enter message: ";
char request[max_length];
std::cin.getline(request, max_length);
size_t request_length = strlen(request);
boost::asio::write(s, boost::asio::buffer(request, request_length));

char reply[max_length];
size_t reply_length = boost::asio::read(s,
boost::asio::buffer(reply, request_length));
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}

boost_server_class.cpp

#include "TCPIP_server.h"
#include <iostream>

int main()
{
std::cout<<"TCPIP_server:"<<std::endl;
TCPIP_server server;

server.main_server();

return 0;
}

TCPIP_server.h

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>

using boost::asio::ip::tcp;

const int max_length = 1024;

class TCPIP_server
{
private:

typedef boost::shared_ptr<tcp::socket> socket_ptr;
char data[max_length];

public:

TCPIP_server(){}
~TCPIP_server(){}
void session(socket_ptr sock)
{
std::cout<<"TCPIP_server session:"<<std::endl;
try
{
for (;;)
{
char data[max_length];

boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.

boost::asio::write(*sock, boost::asio::buffer(data, length));
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void server(boost::asio::io_service& io_service, short port)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));

for (;;)
{
socket_ptr sock(new tcp::socket(io_service));
a.accept(*sock);
boost::bind(&TCPIP_server::session, this, sock);
}
}
void main_server()
{
try
{
std::cerr << "Usage: blocking_tcp_echo_server \n";
boost::asio::io_service io_service;

using namespace std; // For atoi.
server(io_service, atoi("4001"));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
};

最佳答案

你的服务器坏了。

接受连接后,您创建了一个未命名的仿函数,绑定(bind)到永远不会被调用的 TCPIP_server::session()。接受的套接字将立即超出范围,这将关闭它并且您的客户端看到 eof

socket_ptr sock(new tcp::socket(io_service));
a.accept(*sock);
boost::bind(&TCPIP_server::session, this, sock); // <-- this does nothing

您可以通过删除仿函数来解决此问题,并在接受连接后调用 this->session(sock)。但是,我不建议使用这种方法,您的服务器是单线程的,因此在第一个连接后它将无法为其他客户端提供服务。相反,我建议遵循 async echo sever example并使用单个线程、单个 io_service 和异步方法重写您的服务器。

关于c++ - BOOST-ASIO 阻塞服务器不响应阻塞客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801234/

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