Boost logo

Boost Users :

Subject: [Boost-users] ASIO: sending QString (UTF-16)
From: Rangel [LISTAS] (listas_at_[hidden])
Date: 2009-10-21 12:19:50


Hello, I am learning ASIO, and I need to send a QString (from QT) thru
async_write.

The "buffer" function does not supports QString, so I wrote my own, is it
correct? "utf16()" returns a pointer to a ushort (16 bit values).

namespace boost {
        namespace asio {

                inline const_buffers_1 buffer(const QString &data)
                {
                        return const_buffers_1(const_buffer(data.utf16(),
data.size()*sizeof(ushort)));
                }
        }
};

As an example, I am trying to send HTTP header and data, using a simple HTTP
server sample. As the header needs to be ASCII, I am sending a vector, with
a std::string and a QString, is the way it is done right? (The "message_"
had a Japanese string for effective test that was removed to put on this
email)

        ba::ip::tcp::socket socket_;
        std::vector<ba::const_buffer> lbuf;
        static std::string header_;
        static QString message_;

std::string connection::header_="HTTP/1.0 200 OK\r\nContent-Type: text/html;
charset=utf-16\r\n\r\n";

QString
connection::message_=QString::fromUtf16(L"<html><head><title>test</title>"
    L"</head><body><h1>Test</h1><p>This is a test!</p>"
        L"</body></html>");

        void handle_read(const boost::system::error_code& error,
                     size_t bytes_transferred)
        {
                lbuf.clear();
                lbuf.push_back(ba::buffer(header_));
                lbuf.push_back(ba::buffer(message_));
                
                ba::async_write(socket_, lbuf,
                        boost::bind(&connection::handle_write,
shared_from_this(),
                        ba::placeholders::error,
ba::placeholders::bytes_transferred));
        }


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net