开发者

Problems using boost::asio::async_read()

开发者 https://www.devze.com 2023-02-01 06:29 出处:网络
Here\'s the code I use: class Server { ..... void Server::accepted() { std::cout << \"Accepted!\" << std::endl;

Here's the code I use:

class Server
{
.....

void Server::accepted()
{
    std::cout << "Accepted!" << std::endl;

    boost::array<char, 1> buf;
    boost::asio::async_read(socket, boost::asio::buffer(buf),
        boost::bind(&Server::handleRead, this, buf, boost::asio::placeholders::error));
}

void Server::handleRead(boost::array<char, 1> buf, const boost::system::error_code& error)
{
    if(!error)
    {
        std::cout << "Message: " << buf.data() << std::endl;
    }
    else
    {
        std::cout << "Error occurred." << std::endl;
    }
}

.....
}

The problem is that I always get the same data from the client: a specific char. In my client I tried sending other char, but still the server shows the same char.

And when I try to read more than 1 bytes, I 开发者_运维知识库get an error that the buf variable is used before it's initialized.


You're using the local variable buf as the read buffer, which is dangerous and won't work. Also, you're just sending the original contents of that buffer to the handler. So instead, you need to use a buffer with a longer lifetime. Something like this:

class Server
{
.....

boost::array<char, 1> buf;

void Server::accepted()
{
    std::cout << "Accepted!" << std::endl;

    boost::asio::async_read(socket, boost::asio::buffer(buf),
        boost::bind(&Server::handleRead, this, boost::asio::placeholders::error));
}

void Server::handleRead(const boost::system::error_code& error)
{
    if(!error)
    {
        std::cout << "Message: " << buf.data() << std::endl;
    }
    else
    {
        std::cout << "Error occurred." << std::endl;
    }
}

.....
}

edit: or alternatively, using a heap allocated buffer (not sure if the code is right, but you'll get the idea):

void Server::accepted()
{
    std::cout << "Accepted!" << std::endl;

    boost::shared_ptr<boost::array<char, 1>> buf(new boost::array<char, 1>);

    boost::asio::async_read(socket, boost::asio::buffer(*buf),
        boost::bind(&Server::handleRead, this, buf, boost::asio::placeholders::error));
}

void Server::handleRead(boost::shared_ptr<boost::array<char, 1>> buf, const boost::system::error_code& error)
{
    if(!error)
    {
        std::cout << "Message: " << buf->data() << std::endl;
    }
    else
    {
        std::cout << "Error occurred." << std::endl;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号