【问题标题】:Boost async server buffer error提升异步服务器缓冲区错误
【发布时间】:2015-04-14 15:56:40
【问题描述】:

我正在尝试在 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 - 'data' : 不是 'boost::shared_prt' 的成员
  • C2027 - 使用未定义类型 'boost::array'

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

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

提前致谢。

【问题讨论】:

  • 我会说大多数错误都是不言自明的。例如,boost::system::error_code::message 是一个函数,而不是成员变量。而且很容易找到这些错误的位置,因为编译器非常友好地提供了行号,并且可能让您双击实际的错误消息以转到错误的位置。

标签: c++ boost boost-asio


【解决方案1】:
  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));
    

    专业提示: 考虑在缓冲区中使用unsigned char,以避免在传递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;
    

然后编译。不能保证它会达到你的预期(我还没有阅读代码)

【讨论】:

  • 非常感谢。您的回答完全修复了我所有的错误。 === 构建:1 成功= ===
猜你喜欢
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多