boost asio 中具有异步功能的不良字符

发布于 2024-12-15 14:37:54 字数 1716 浏览 2 评论 0原文

我在 boost::asio 中遇到数据传输问题。 使用以下代码:

Server.cpp

void        Server::accept(void)
{
    Network::ptr connection = Network::create(this->my_acceptor.get_io_service());
    this->my_acceptor.async_accept(connection->getSocket(), bind(&Server::endCmd, this, connection, placeholders::error));
}

void        Server::endCmd(Network::ptr connection, const boost::system::error_code& error)
{
    if (!error)
        {
            connection->Swrite("BIENVENUE");
            this->accept();
        }
}

Network.cpp

void        Network::Sread(void)
{
    async_read(this->socket, buffer(this->rbuffer), bind(&Network::endRead, shared_from_this(), placeholders::error));
}

void            Network::endRead(const error_code& error)
{
    if (!error)
        {
            this->rcv_msg = this->rbuffer.c_array();
            std::cout << this->rcv_msg << std::endl;
            this->Sread();
        }
}

void            Network::Swrite(std::string msg)
{
    this->msg = msg;
    async_write(this->socket, buffer(this->msg, (int)this->msg.size()), bind(&Network::endWrite, shared_from_this(), placeholders::error));
}

void            Network::endWrite(const error_code &error)
{
    if (!error)
    {
        this->Sread();
    }
}

tcp::socket&    Network::getSocket(void)
{
    return (this->socket);
}

Network::ptr                Network::create(io_service &ios)
{
    return (ptr(new Network(ios)));
}

当我使用 telnet 向服务器发送类似“Hello world”的字符串时,他写入以下内容: 我的服务器的错误

谁能告诉我为什么服务器会写入许多未知字符?

i have a problem with data transfert in boost::asio.
with the following code :

Server.cpp

void        Server::accept(void)
{
    Network::ptr connection = Network::create(this->my_acceptor.get_io_service());
    this->my_acceptor.async_accept(connection->getSocket(), bind(&Server::endCmd, this, connection, placeholders::error));
}

void        Server::endCmd(Network::ptr connection, const boost::system::error_code& error)
{
    if (!error)
        {
            connection->Swrite("BIENVENUE");
            this->accept();
        }
}

Network.cpp

void        Network::Sread(void)
{
    async_read(this->socket, buffer(this->rbuffer), bind(&Network::endRead, shared_from_this(), placeholders::error));
}

void            Network::endRead(const error_code& error)
{
    if (!error)
        {
            this->rcv_msg = this->rbuffer.c_array();
            std::cout << this->rcv_msg << std::endl;
            this->Sread();
        }
}

void            Network::Swrite(std::string msg)
{
    this->msg = msg;
    async_write(this->socket, buffer(this->msg, (int)this->msg.size()), bind(&Network::endWrite, shared_from_this(), placeholders::error));
}

void            Network::endWrite(const error_code &error)
{
    if (!error)
    {
        this->Sread();
    }
}

tcp::socket&    Network::getSocket(void)
{
    return (this->socket);
}

Network::ptr                Network::create(io_service &ios)
{
    return (ptr(new Network(ios)));
}

When i send a string like "Hello world" to the server with telnet, he write the following content : bug with my serveur

Who can tell me why the server is writting many unknow characters ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以畏孤独 2024-12-22 14:37:54

我相信你的 readHandler (Network::endRead()) 缺少一个参数。无法确定从流中读取了多少字节。我的回调通常如下所示(但我调用 async_read_some()):

void HandleAsioRead(const boost::system::error_code& rErrorCode,
                    std::size_t nBytesTransferred)

然后我会在打印之前以 null 终止从流中读取的字符串。

编辑:
我对 async_read_some() 的调用如下所示:

    m_Socket.async_read_some(
        asio::buffer(m_RawBuffer, MaxBufferSize),
        bind(&HandleAsioRead,
             this,
             asio::placeholders::error,
             asio::placeholders::bytes_transferred));

I believe your readHandler (Network::endRead()) is missing an argument. There is no way to determine how many bytes were read from the stream. My callback usually looks like this (but I call async_read_some()):

void HandleAsioRead(const boost::system::error_code& rErrorCode,
                    std::size_t nBytesTransferred)

Then I would null terminate the string I've read from the stream before printing.

Edit:
My call to async_read_some() looks like this:

    m_Socket.async_read_some(
        asio::buffer(m_RawBuffer, MaxBufferSize),
        bind(&HandleAsioRead,
             this,
             asio::placeholders::error,
             asio::placeholders::bytes_transferred));
治碍 2024-12-22 14:37:54

看起来 rbuffer.c_array() 不是一个以 null 结尾的字符串,而只是一个字符数组。将字符数组打印到 cout 假定数组以 null 终止,在这种情况下,这会导致数组末尾之后的内存包含在输出中。

在尝试打印之前,您应该从数据创建一个 std::string :(

std::string received(rbuffer.c_array(), rbuffer.size());
std::cout << received;

假设 rbuffer 有一个 size() 方法或等效方法)

It looks like the rbuffer.c_array() isn't a null terminated string but just a array of characters. Printing a character array to cout assumes that the array is null terminated, which in this case causes the memory after the end of the array being included in the output.

You should create a std::string from the data before trying to print it:

std::string received(rbuffer.c_array(), rbuffer.size());
std::cout << received;

(Assuming rbuffer has a size() method or something equivalent)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文