简体   繁体   English

Boost Asio异步:服务器向客户端发送消息

[英]Boost Asio asynchronous: Server sent message to client

I am working on a Server/Client application. 我正在处理服务器/客户端应用程序。 Client send a message (header+body) to the Server. 客户端向服务器发送消息(标头+正文)。 The server receives the message, make some modifications and sent it back. 服务器接收到该消息,进行一些修改并将其发送回去。

So the client sends the message. 因此,客户端发送消息。

async_write (m_socket,boost::asio::buffer(msg -> HeaderData() , msg -> SendLength ()),
    boost::bind ( &Client::HandleSentMessage , this ,
    boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred));

The server receives the message. 服务器收到消息。

m_socket.async_read_some ( boost::asio::buffer ( *receiveBuffer ) , boost::bind( 
     &ASyncConnectionMT::HandleReceived , shared_from_this() , receiveBuffer , 
     boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred));

It goes to my function: 这取决于我的功能:

void HandleReceived ( BufferPtr receiveBuffer , const boost::system::error_code& ec , size_t size)

And there it changes the message received. 并在那里更改收到的消息。

for ( size_t i=0 ; i!=size ; i++ )
    {
         ((*sendBuffer)[i]) = toupper((*receiveBuffer)[i]); 
    }

And sends it back. 并发回。

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));

So far all good. 到目前为止一切都很好。 The code is running flawlessly. 该代码可以完美运行。 But what is my problem. 但是我有什么问题。 I receive the send message and modify it but, lets say for example I want to send back something else. 我收到并修改了发送消息,但是,例如,我想发送其他消息。 For example a string. 例如一个字符串。

The problem is that I have to pass buffer as shared_ptr so my object won't be destroyed after leaving this function. 问题是我必须将缓冲区作为shared_ptr传递,这样我的对象在离开此函数后不会被破坏。

typedef boost::array < char , 65536 > Buffer;
typedef boost::shared_ptr < Buffer > BufferPtr;
BufferPtr receiveBuffer ( new Buffer ) ;
BufferPtr sendBuffer ( new Buffer ) ;

I want the client to send me a message like "I am ready" and the server to send back some information. 我希望客户端向我发送诸如“我已经准备好”之类的消息,并让服务器向我发送一些信息。 After the client receives the information, to send a new message to the client that he is ready. 客户端收到信息后,向客户端发送一条新消息,表明他已准备就绪。 So the server can send the next piece of informations. 因此服务器可以发送下一条信息。 Tried all day, I just can't figure out how to send a string or a char array to client. 经过一整天的尝试,我只是想不通如何向客户端发送字符串或char数组。

Can you show me how to send a string or a char array or to convert them to a shared_ptr ? 您能告诉我如何发送字符串或char数组或将它们转换为shared_ptr吗?

You can copy a string to shared pointer like this if you have c++11: 如果您拥有c ++ 11,则可以将字符串复制到共享指针,如下所示:

auto shared_string = std::make_shared<std::string>(string_to_send);

Or with boost: 或提升:

boost::shared_ptr<std::string> ptr = boost::make_shared<std::string>(string_to_send);

And than send it to socket: 然后将其发送到套接字:

async_write(m_socket, boost::asio::buffer(*shared_string), m_strand.wrap(boost::bind(&ASyncConnectionMT::HandleSent, shared_from_this() , shared_string, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));

Note that usualy it is required to send some kind of header, so the client can find out the length of the string. 请注意,通常需要发送某种报头,以便客户端可以找出字符串的长度。 So you can copy string length to boost::array and send it to client first, and than send the string itself, for example. 因此,您可以将字符串长度复制到boost::array ,然后先将其发送给客户端,然后再发送字符串本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM