gpt4 book ai didi

c++ - 异步读取服务器 BoostAsio 不调用处理程序

转载 作者:行者123 更新时间:2023-11-30 05:37:27 26 4
gpt4 key购买 nike

我目前正在使用 Boost::Asio 做一个基本的读/写服务器,当涉及到库的 async_read 函数的使用时,我遇到了一个小问题。

这是我的代码片段:

ma​​in.cpp :

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "TCPServer.hpp"
#include "TCPConnection.hpp"

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

int main()
{
try
{
boost::asio::io_service io_service;
TCPServer server(io_service);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}

return 0;
}

TCPServer.hpp :

#ifndef TCPSERVER_HPP_
#define TCPSERVER_HPP_

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include "TCPConnection.hpp"

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

class TCPServer
{
private:
tcp::acceptor _acceptor;

public:
TCPServer(boost::asio::io_service& ioService)
: _acceptor(ioService, tcp::endpoint(tcp::v4(), 4040))
{
startAccept();
}

private:
void startAccept()
{
TCPConnection::pointer newConnection =
TCPConnection::create(_acceptor.get_io_service());
_acceptor.async_accept(newConnection->getSocket(),
boost::bind(&TCPServer::handleAccept, this, newConnection,
boost::asio::placeholders::error));
}

void handleAccept(TCPConnection::pointer newConnection, const boost::system::error_code& error)
{
if (!error)
{
newConnection->asyncWrite("JIOLE");
startAccept();
}
}
};

#endif

TCPConnection.hpp :

#ifndef TCPCONNECTION_HPP_
#define TCPCONNECTION_HPP_

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

class TCPConnection : public boost::enable_shared_from_this<TCPConnection>
{
private:
boost::asio::ip::tcp::socket _socket;
std::string _readMessage;
boost::asio::streambuf _response;

public:
typedef boost::shared_ptr<TCPConnection> pointer;

static pointer create(boost::asio::io_service& ios)
{
return pointer(new TCPConnection(ios));
}

boost::asio::ip::tcp::socket& getSocket()
{
return _socket;
}

void asyncWrite(const std::string &message)
{
boost::asio::async_write(_socket,
boost::asio::buffer(message),
boost::bind(&TCPConnection::handleWrite, shared_from_this(),


boost::asio::placeholders::error));
std::cout << "AsyncWrite" << std::endl;
}

void asyncRead()
{
std::cout << "1st \"asyncRead\"" << std::endl;
boost::asio::async_read(_socket,
_response,
boost::bind(&TCPConnection::handleRead, shared_from_this(),
boost::asio::placeholders::error));
std::cout << "2nd \"asyncRead\"" << std::endl;
}

void close()
{
_socket.close();
}

private:

TCPConnection(boost::asio::io_service &ioS) : _socket(ioS) {}

void handleWrite(const boost::system::error_code& error)
{
std::cout << "Write Handler" << std::endl;
if (!error)
{
asyncRead();
}
//SEE WHAT TO DO IN CASE OF ERROR
}

void handleRead(const boost::system::error_code& error)
{
std::cout << "Read Handler" << std::endl;
if (!error)
{
std::cout << &_response << std::endl;
asyncRead();
}
else
{
std::cout << &error << std::endl;
_socket.close();
}
//CREATE SENDER(RSEPONSE::ERROR)
}

};

#endif

问题是我的 async_read 没有调用处理程序。这是输出:

输出:

AsyncWrite
Write Handler
1st "asyncRead"
2nd "asyncRead"

当我在 NetCat 客户端上写东西时,没有收到任何东西。

当我按下 ctrl+C 时会发生什么:

Read Handler
0x7fff645f3be0

我不明白为什么什么都没有收到。

最佳答案

问题是你没有告诉 async_read 什么时候 它应该调用处理函数。有两种方法可以指定它:指定输入大小,然后调用处理程序或通过 async_read_until 指定分隔符。 .

在您的情况下,您可以使用以下内容:

boost::asio::async_read_until(_socket,
_response,
"\n",
boost::bind(&TCPConnection::handleRead,
shared_from_this(),
boost::asio::placeholders::error)
);

这将在从客户端发送换行符时调用处理程序。

关于c++ - 异步读取服务器 BoostAsio 不调用处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33198745/

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