gpt4 book ai didi

c++: BOOST_ASIO 服务器回复不符合预期?

转载 作者:行者123 更新时间:2023-11-28 07:40:15 25 4
gpt4 key购买 nike

基本上我有一个客户端,我向服务器发送一个 10 字节的字符串 hellohello 并且在服务器内我希望回复为 0123456789 返回给客户端但是相反,我又得到了 hellohello 吗?我在 tcp_server.cpp 的第 58 行将 char data_ 更改为 char data_out 因为我认为那是发送数据包数据的地方?我很确定它会被调用,但由于某些原因,事情并没有像我想的那样工作。

这是服务器输出,

handle read: bytes_transferred10
10
handle write:
0123456789
handle read: bytes_transferred0

我也想知道为什么 handle read: bytes_transferred0 又被调用了?

这是客户端输出,

Enter message: hellohello
Reply is: hellohello

Process returned 0 (0x0) execution time : 6.484 s
Press any key to continue.

这是 tcp_server.cpp

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

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

class session
{
public:
session(boost::asio::io_service& io_service)
: socket_(io_service)
{
}

tcp::socket& socket()
{
return socket_;
}

void start()
{
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred);


private:
void handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
std::cout<<"handle read: bytes_transferred"<<bytes_transferred<<std::endl;
if (!error)
{
read_handler(error, bytes_transferred);
boost::asio::async_write(socket_,
boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else
{
delete this;
}
}

void handle_write(const boost::system::error_code& error)
{
std::cout<<"handle write: "<<std::endl;
data_out = {'0','1','2','3','4','5','6','7','8','9'};
if (!error)
{
std::cout<<data_out<<std::endl;

socket_.async_read_some(boost::asio::buffer(data_out, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
delete this;
}
}

tcp::socket socket_;
enum { max_length = 1024 };
char data_[max_length];
char data_out[max_length];
};
void session::read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
std::cout<<bytes_transferred<<std::endl;
}

class server
{
public:
server(boost::asio::io_service& io_service, short port)
: io_service_(io_service),
acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
{
start_accept();
}


private:
void start_accept()
{
session* new_session = new session(io_service_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}

void handle_accept(session* new_session,
const boost::system::error_code& error)
{
if (!error)
{
new_session->start();
}
else
{
delete new_session;
}

start_accept();
}

boost::asio::io_service& io_service_;
tcp::acceptor acceptor_;
};

int main()
{
try
{
boost::asio::io_service io_service;
server s(io_service, 4000);

io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}

tcp_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
{


boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), "127.0.0.1", "4000");
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;
}

最佳答案

你的评论

I think handle_read() calls handle_write() recursively?

很接近但不太正确,因为这里没有递归。 documentation 很好地解释了这一点:

Regardless of whether the asynchronous operation completes immediately or not, the handler will not be invoked from within this function. Invocation of the handler will be performed in a manner equivalent to using boost::asio::io_service::post().

额外的强调是我的。与递归不同,最好将这些概念视为链接,因为一个操作(例如 async_write())是在另一个操作(例如 async_read())中启动的。确切的细节取决于所使用的协议(protocol)。

如果您希望服务器向客户端发送字符串 0123456789,请在调用 async_write() 之前填充您的缓冲区。

关于c++: BOOST_ASIO 服务器回复不符合预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983311/

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