提升异步服务器缓冲区错误

Boost async server buffer error

我正在尝试在 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 ; 
                }
            }

};

我收到以下错误。

提前致谢。

  1. 您错过了包含

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

    ((&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;
    

然后编译。不能保证它会如您所愿(我还没有阅读代码)