提升Asio - 客户端到服务器奇怪的输出

时间:2014-12-04 18:15:50

标签: asynchronous boost boost-asio

我在使用Boost asio的数据包时遇到问题,让我解释一下。

    #include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include "../configuration/constants.h" // port = 2013 ; maxLength = 512

class client : public std::enable_shared_from_this<client>
{
public:
    client(boost::asio::ip::tcp::socket socket) : m_socket(std::move(socket)){}

    void start()
    {
        read();
    }

private:
    void read()
    {
        auto self(shared_from_this());
        m_socket.async_read_some(boost::asio::buffer(m_data, sizeof(m_data)), [this, self](boost::system::error_code error, std::size_t length)
        {
            if (!error)
            {
                std::cout << m_data;
                send(length);
            }
        });
    }

    void send(std::size_t length)
    {
        auto self(shared_from_this());
        boost::asio::async_write(m_socket, boost::asio::buffer(m_data, length), [this, self](boost::system::error_code error, std::size_t)
        {
            if (!error)
            {
                start();
            }
        });
    }

    boost::asio::ip::tcp::socket m_socket;
    char m_data[maxLength];
};

class server
{
public:
    server(boost::asio::io_service& ios, unsigned short port) : m_acceptor(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)), m_socket(ios)
    {
        acceptConnection();
    }

private:
    void acceptConnection()
    {
        m_acceptor.async_accept(m_socket, [this](boost::system::error_code error)
        {
            if (!error)
                std::make_shared<client>(std::move(m_socket))->start();
            acceptConnection();
        });
    }
    boost::asio::ip::tcp::acceptor m_acceptor;
    boost::asio::ip::tcp::socket m_socket;
};

void connexionTCP()
{
    try
    {
        std::cout << "TCP open" << std::endl;
        boost::asio::io_service iosConnector;
        server server(iosConnector, port); // port = 2013
        iosConnecteur.run();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception : " << e.what() << "\n";
    }
}

如果我尝试运行它,TCP open就是输出。如果客户端发送“Hi”,则输出(在控制台中)将为:

  

HiÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͲ²²²½½½½½½½½

最后一个代码已完成。任何的想法 ?顺便说一下,当我使用puTTY作为客户端时,当我发送“Hi”时,我会收到“Hi”。这只是在控制台中给我写这个。

提前致谢

2 个答案:

答案 0 :(得分:0)

在打印之前将'\0'放在角色阵列的末尾:)

if (!error)
{
  m_data[length] = '\0';
  std::cout << m_data;
  send(length);
}

答案 1 :(得分:0)

嗯,我做到了。 当我读到某些东西时,数据长度是变量size_t长度。 所以,从中我做到了:

if (!error) // If there is no error
    {
        for (int i = 0; i < (length); i++) // If i < length
        {
            std::cout << m_data[i]; // Print every char since i < length
            if (i == length) // Just to make the output more presentable
                std::cout << "\n"; // Enter
        }
    start(); // Back to the begining.
}

输入:“嗨” 输出:“嗨”\ n

感谢您的帮助,Drax和sehe。见到你。

我现在遇到的问题是我无法在std :: string中存储m_data,因为这是for。我尝试了矢量,但没办法。我想得到它,因为它比使用字符串更容易使用字符串。 所以我想要做的是:在字符串中获取for的输出m_data。