服务器和客户端之间如何异步通信
How to asynchronously communicate between server and client
好的,此代码处理服务器和客户端之间的服务器端通信。我不太熟悉boost::asio,但是我已经尽力按照几个教程进行了。但是,尝试在我的客户端(我还没有构建它,所以我正在使用腻子)和服务器之间进行通信时仍然存在一个问题。服务器仅在客户端关闭连接时才接收数据。我只是想知道是否是这种情况,如果是这样,我该如何响应客户端请求?
这是启动服务器的函数(缩短了,我去掉了错误处理机制)
void Initialize(){
boost::asio::io_service ioservice;
Network network(ioservice);
ioservice.run();
}
这是开始接受连接的函数
void Network::StartAccept(){
Connection::pointer new_connection = Connection::create(acceptor_.get_io_service());
acceptor_.async_accept(new_connection->getSocket(),
boost::bind(&Network::HandleAccept, this,
new_connection, boost::asio::placeholders::error));
}
此函数处理接受并再次启动另一个接受
void Network::HandleAccept(Connection::pointer new_connection, const boost::system::error_code & error){
if (!error)
{
//sio::cout() is one of my functions that automatically time stamps everything and ends the line if needed
sio::cout("Client Connected from Origin: ", false);
sio::cout(new_connection->getSocket().remote_endpoint().address().to_string(), true, false);
new_connection->start();
}
else {
sio::cout("Could Not Accept Connection to Client");
}
StartAccept();
}
这个函数处理客户端I/O
void Connection::start() {
//message_ = "Hello World";
//boost::asio::async_write(socket_, boost::asio::buffer(message_), boost::bind(&Connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
boost::asio::async_read(socket_, msg, boost::bind(&Connection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
我希望能够将 "hello world" 写入客户端,然后读取客户端所说的内容,但是如果我这样做 async_write
(已注释掉),它会断开客户端连接async_read
吐出一个错误,因为没有连接
TLDR 我如何保持连接打开,或者我不应该
我解决了我的问题,本来我想让客户端发送信息到服务器然后服务器响应。我想出了怎么做。 @Arunmu 帮助我发现 PuTTY 导致了一些问题,因为当他指出它时我更深入地研究了它。
原来 PuTTY 正在关闭 window 并且不显示服务器端消息,因为它认为退出成功。我唯一需要更改的是我发布的最后一个功能。
void Connection::start() {
boost::asio::async_read_until(socket_, msg, "\n",
boost::bind(&Connection::handle_read, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
该功能至少在某种程度上符合我的要求。我必须编写的客户端每次想要向服务器询问某事时都必须创建一个新连接。
好的,此代码处理服务器和客户端之间的服务器端通信。我不太熟悉boost::asio,但是我已经尽力按照几个教程进行了。但是,尝试在我的客户端(我还没有构建它,所以我正在使用腻子)和服务器之间进行通信时仍然存在一个问题。服务器仅在客户端关闭连接时才接收数据。我只是想知道是否是这种情况,如果是这样,我该如何响应客户端请求?
这是启动服务器的函数(缩短了,我去掉了错误处理机制)
void Initialize(){
boost::asio::io_service ioservice;
Network network(ioservice);
ioservice.run();
}
这是开始接受连接的函数
void Network::StartAccept(){
Connection::pointer new_connection = Connection::create(acceptor_.get_io_service());
acceptor_.async_accept(new_connection->getSocket(),
boost::bind(&Network::HandleAccept, this,
new_connection, boost::asio::placeholders::error));
}
此函数处理接受并再次启动另一个接受
void Network::HandleAccept(Connection::pointer new_connection, const boost::system::error_code & error){
if (!error)
{
//sio::cout() is one of my functions that automatically time stamps everything and ends the line if needed
sio::cout("Client Connected from Origin: ", false);
sio::cout(new_connection->getSocket().remote_endpoint().address().to_string(), true, false);
new_connection->start();
}
else {
sio::cout("Could Not Accept Connection to Client");
}
StartAccept();
}
这个函数处理客户端I/O
void Connection::start() {
//message_ = "Hello World";
//boost::asio::async_write(socket_, boost::asio::buffer(message_), boost::bind(&Connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
boost::asio::async_read(socket_, msg, boost::bind(&Connection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
我希望能够将 "hello world" 写入客户端,然后读取客户端所说的内容,但是如果我这样做 async_write
(已注释掉),它会断开客户端连接async_read
吐出一个错误,因为没有连接
TLDR 我如何保持连接打开,或者我不应该
我解决了我的问题,本来我想让客户端发送信息到服务器然后服务器响应。我想出了怎么做。 @Arunmu 帮助我发现 PuTTY 导致了一些问题,因为当他指出它时我更深入地研究了它。
原来 PuTTY 正在关闭 window 并且不显示服务器端消息,因为它认为退出成功。我唯一需要更改的是我发布的最后一个功能。
void Connection::start() {
boost::asio::async_read_until(socket_, msg, "\n",
boost::bind(&Connection::handle_read, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
该功能至少在某种程度上符合我的要求。我必须编写的客户端每次想要向服务器询问某事时都必须创建一个新连接。