gpt4 book ai didi

c++ - boost 异步服务器缓冲区错误

转载 作者:行者123 更新时间:2023-11-30 05:47:42 25 4
gpt4 key购买 nike

我正在尝试在 boost asio 中制作服务器和客户端。目前我收到此错误。你能指出我做错了什么吗?

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>


// Forward declaration
class ASyncConnectionMT;
class ASyncEchoServerMT;

// Typedef for the buffer type (shared_ptr)
typedef boost::array < char , 65536 > Buffer;
typedef boost::shared_ptr < Buffer > BufferPtr;

// Typedef for the ASyncConnectionMT shared_ptr
// Typedef for the ASyncEchoServerMT shared_ptr
// Derived from "enable_shared_from_this" so the 'this' object can
// be passed as shared_ptr to the callback function
typedef boost::shared_ptr < ASyncConnectionMT > ASyncConnectionMTPtr;
typedef boost::shared_ptr < ASyncEchoServerMT > ASyncEchoServerMTPtr;


// Class the handles the client
class ASyncConnectionMT : public::boost::enable_shared_from_this<ASyncConnectionMT>
{

private: // The socket class for communication.
boost::asio::ip::tcp::socket m_socket;

// Strand object to synchronise calling handlers. Multiple threads might acces the socked
// at the same time since send and recieve are started asynchronously at the same time
boost::asio::strand m_strand;

public: // Constructor with the IO service to use
ASyncConnectionMT ( boost::asio::io_service& ios) : m_socket(ios), m_strand (ios)
{
}

// Retrieve the socked used by this connection
// Need to be passed to acceptor.accept() function
boost::asio::ip::tcp::socket& Socket()
{
return m_socket;
}

// Start handling the connection
void Start()
{
std:: cout << m_socket.remote_endpoint() << ": Connection accepted" << std:: endl ;
StartReceiving() ;
}

// Start receiving data
void StartReceiving()
{
// Create receive buffer
BufferPtr receiveBuffer ( new Buffer ) ;

// Start async read, must pass 'this' as shared_ptr, else the
// 'this' object will be destroyed after leaving this function
async_write ( m_socket, boost::asio::buffer ( *receiveBuffer ) , m_strand.wrap ( boost::bind ( &ASyncConnectionMT::HandleReceived, shared_from_this() ,
receiveBuffer , boost::asio::placeholders::error ,
boost::asio::placeholders::bytes_transferred ) ) ) ;
}


// Handle received data
void HandleReceived ( BufferPtr receiveBuffer , const boost::system::error_code& ec , size_t size)
{

if (!ec)
{
// Print received message
std:: cout << m_socket.remote_endpoint() << ": Message received: " << std:: string (receiveBuffer -> data() , size ) << std:: endl ;

// Convert to uppercare. We can't use the same buffer because that could be
// overwritten by another recieve

// UPD -> boost shared_ptr<TBuffer> sendBuffer(new TBuffer());
BufferPtr sendBuffer ( new Buffer ) ;
for ( size_t i=0 ; i!=size ; i++ )
{
(( &sendBuffer )[i]) = toupper (( &receiveBuffer )[i]) ;
}

// Start sending reply, must pass 'this' as shared_ptr, else the 'this' object will be
// destroyed after leaving this function. We pass the buffer as shared_ptr to the handler
// so the buffer is still in memory after sending is complete. Without it, the buffer could
// be deleted before the send operation is complete. The Handle Set is now synchronised via the strand.
async_write ( m_socket, boost::asio::buffer ( *sendBuffer , size ) , m_strand.wrap ( boost::bind ( &ASyncConnectionMT::HandleSent ,
shared_from_this() , sendBuffer ,
boost::asio::placeholders::error ,
boost::asio::placeholders::bytes_transferred ) ) ) ;

// Start receiving next bit

StartReceiving() ;

}

else if ( ec == boost::asio::error::eof)
{
// Client disconnected. Close the socket.
std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
m_socket.close();
}

}

// Handle for when the data si sent
void HandleSent ( BufferPtr sendBuffer , const boost::system::error_code& ec , size_t size)
{

if (!ec)
{
// Start receiving again
StartReceiving() ;

}

else if ( ec == boost::asio::error::eof)
{
// Client disconnected. Close the socket.
std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
m_socket.close();
}

else
{
std:: cout << "Error: " << ec.message << std:: endl ;
}
}

};

我收到以下错误。

  • C3967 - boost::system::error_code::message':函数调用缺少参数列表;使用 '&'boost::system::error_code::message' 创建指向成员的指针
  • C2664 - “toupper”:无法将参数 1 从“BufferPtr”转换为“int”
  • C2512 - “boost::array”:没有合适的默认构造函数可用
  • C2039 - “数据”:不是“boost::shared_prt”的成员
  • C2027 - 使用未定义类型“boost::array”

  • 不允许指向不完整的类类型

  • 不存在从“BufferPtr”到“int”的合适的转换函数
  • 不允许不完整的类型

提前致谢。

最佳答案

  1. 你错过了包含

    #include <boost/array.hpp>
  2. topper 循环错误

    ((&sendBuffer)[i]) = toupper((&receiveBuffer)[i]);

    应该更像

    ((*sendBuffer)[i]) = toupper((*receiveBuffer)[i]);

    甚至更像

    std::transform(receiveBuffer->begin(), receiveBuffer->end(), sendBuffer->begin(), static_cast<int(&)(int)>(std::toupper));

    Pro tip: consider using unsigned char in the buffer to avoid unwanted sign extension when passing int to_upper

    ((*sendBuffer)[i]) = toupper(static_cast<unsigned char>((*receiveBuffer)[i]));
  3. 如评论(和编译器消息)所说:

            std::cout << "Error: " << ec.message << std::endl;

    应该是

            std::cout << "Error: " << ec.message() << std::endl;

然后编译。不能保证它会按照您的预期进行(我还没有阅读代码)

关于c++ - boost 异步服务器缓冲区错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502469/

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