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

  显示原文与译文双语对照的内容
0 0

我想让一个&客户成为 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:: 消息': 函数调用缺少参数列表;使用'&'。error_code:::: 消息',用于创建指向成员的指针
  • C2664 -'toupper': 无法将 parametor 1从'bufferPtr'转换为'"'
  • C2512 -'boost:: 数组': 没有可用的默认构造函数
  • C2039 -'数据': 不是 boost:: shared_prt'的成员'
  • C2027 - 使用未定义类型':: array''

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

  • 不存在从"bufferPtr"到"int"的合适转换函数
  • 不允许不完全类型

先谢谢了。

时间: 原作者:

0 0
  1. 你错过了

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

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

然后编译,不保证它做你所期望的( 我没有读过代码)

原作者:
...