即使在写入句柄完成之后,Boost :: Asio Server也会从客户端读取空白消息

时间:2016-08-20 22:46:42

标签: c++ boost client boost-asio

我尝试从客户端代码向服务器写入异步消息,写入处理程序,并发送正确的字节;但是,服务器接收0个字节。

客户输出: 你有联系 您从服务器收到以下消息: 2016年8月20日星期六17:42:01 发送...

服务器输出: 服务器在线! 127.0.0.1:51973已连接! 客户已收到消息。 您从服务器收到以下消息:

服务器来源:

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&now);
}


class tcp_connection: public boost::enable_shared_from_this<tcp_connection>
{
public:
    typedef boost::shared_ptr<tcp_connection> pointer;

    static pointer create(boost::asio::io_service& io_service)
    {
        return pointer(new tcp_connection(io_service));
    }

    tcp::socket& socket()
    {
        return socket_;
    }

    // Call boost::asio::async_write() to serve the data to the client.
    // We are using boost::asio::async_write(),
    // rather than ip::tcp::socket::async_write_some(),
    // to ensure that the entire block of data is sent.

    void start()
    {
        // The data to be sent is stored in the class member m_message
        // as we need to keep the data valid
        // until the asynchronous operation is complete.

        m_message = make_daytime_string();

        // When initiating the asynchronous operation,
        // and if using boost::bind(),
        // we must specify only the arguments
        // that match the handler's parameter list.
        // In this code, both of the argument placeholders
        // (boost::asio::placeholders::error
        // and boost::asio::placeholders::bytes_transferred)
        // could potentially have been removed,
        // since they are not being used in handle_write().

        std::cout << socket_.remote_endpoint().address().to_string() << ":" << socket_.remote_endpoint().port() << " connected!" << std::endl;

        boost::asio::async_write(socket_, boost::asio::buffer(m_message),
                                 boost::bind(&tcp_connection::handle_write, shared_from_this()));

        boost::asio::async_read(socket_, boost::asio::buffer(_buffer), boost::bind(&tcp_connection::handle_receive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }


private:
    tcp_connection(boost::asio::io_service& io_service)
    : socket_(io_service)
    {
    }
    // handle_write() is responsible for any further actions
    // for this client connection.

    void handle_write() // call back.. when it finishes sending, we come here
    {
        std::cout << "Client has received the messaged. " << std::endl;
    }

    void handle_receive(const boost::system::error_code& ErrorCode, std::size_t bytes_transferred)
    {
        std::cout << "You received the following message from the server:" << std::endl;
        std::cout.write(_buffer.data(), bytes_transferred);

    }

    tcp::socket socket_;
    std::string m_message;
    boost::array<char, 126> _buffer;
};

class tcp_server
{
public:
    tcp_server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 7171))
    {
        // start_accept() creates a socket and
        // initiates an asynchronous accept operation
        // to wait for a new connection.
        start_accept();
    }

private:
    void start_accept()
    {
        // creates a socket
        tcp_connection::pointer new_connection = tcp_connection::create(acceptor_.get_io_service());

        // initiates an asynchronous accept operation
        // to wait for a new connection.
        acceptor_.async_accept(new_connection->socket(),
                               boost::bind(&tcp_server::handle_accept, this, new_connection,
                                           boost::asio::placeholders::error));
    }

    // handle_accept() is called when the asynchronous accept operation
    // initiated by start_accept() finishes. It services the client request
    void handle_accept(tcp_connection::pointer new_connection,
                       const boost::system::error_code& error)
    {
        if (!error)
        {
            new_connection->start();
        }

        // Call start_accept() to initiate the next accept operation.
        start_accept();
    }

    tcp::acceptor acceptor_;
};

int main()
{
    std::cout << "Server is online!" << std::endl;
    try
    {
        boost::asio::io_service io_service;

        tcp_server server(io_service);

        io_service.run();

    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

客户来源:

#include <iostream>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&now);
}

class Connection
{
public:
    Connection(boost::asio::io_service& io) : _socket(io){}

    void connect(tcp::resolver::iterator& point)
    {
        boost::asio::async_connect(_socket, point, boost::bind(&Connection::onConnected, this, boost::asio::placeholders::error));
    }

    void onConnected(const boost::system::error_code& ErrorCode)
    {
        std::cout << "You are connected" << std::endl;

        // receive first message on onReceive
        boost::asio::async_read(_socket, boost::asio::buffer(_buffer), boost::bind(&Connection::onReceive, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }

    void onSend(const boost::system::error_code& ErrorCode, std::size_t bytes_transferred)
    {
        std::cout << "Sending..." << std::endl;
    }

    void onReceive(const boost::system::error_code& ErrorCode, std::size_t bytes_transferred)
    {
        std::cout << "You received the following message from the server:" << std::endl;
        std::cout.write(_buffer.data(), bytes_transferred);

        // send first message on onSend
        m_message = make_daytime_string();
        boost::asio::async_write(_socket, boost::asio::buffer(m_message), boost::bind(&Connection::onSend, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }

    tcp::socket& getSocket()
    {
        return _socket;
    }
private:
    tcp::socket _socket;
    boost::array<char, 126> _buffer;
    std::string m_message;
};

int main()
{
    try
    {
        boost::asio::io_service io_service;

        tcp::resolver resolver(io_service);

        tcp::resolver::query query("127.0.0.1", "7171");

        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

        Connection conn(io_service);
        conn.connect(endpoint_iterator);

        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

@edit:

新客户代码:

    class Connection : public boost::enable_shared_from_this<Connection>
{
public:
    typedef boost::shared_ptr<Connection> pointer;

    static pointer create(boost::asio::io_service& io_service)
    {
        return pointer(new Connection(io_service));
    }


    tcp::socket& socket()
    {
        return _socket;
    }

    void connect(tcp::resolver::iterator& point)
    {
        boost::asio::async_connect(_socket, point, boost::bind(&Connection::connect_handler, this, boost::asio::placeholders::error));
    }

    void connect_handler(const boost::system::error_code& error)
    {
        if(error)
        {
            std::cout << "Error on connect_handler: " << error.message() << std::endl;
            return;
        }

        std::cout << "You are connected to the server." << std::endl;
        start();

    }

    void start()
    {
        start_write();
        start_read();
    }

private:
    // private ctor
    Connection(boost::asio::io_service& io) : _socket(io){}

    void start_write()
    {
        _daymessage = make_daytime_string();

        boost::asio::async_write(_socket, boost::asio::buffer(_daymessage),
                                 boost::bind(&Connection::handle_write, shared_from_this(),
                                             boost::asio::placeholders::error,
                                             boost::asio::placeholders::bytes_transferred));
    }

    void handle_write(const boost::system::error_code& error,
                      size_t bytes)
    {
        if(error)
        {
            std::cout << "Error on handle write: " << error.message() << std::endl;
            return;
        }

        std::cout << "Message has been sent!" << std::endl;
        start_write();
    }

    void start_read()
    {
        // Start an asynchronous operation to read a newline-delimited message.
        boost::asio::async_read_until(_socket, _buffer, '\n',
                                      boost::bind(&Connection::handle_read, shared_from_this(),
                                                  boost::asio::placeholders::error,
                                                  boost::asio::placeholders::bytes_transferred));
    }

    void handle_read(const boost::system::error_code& error, size_t bytes)
    {
        if(error)
        {
            std::cout << "Error on handle read: " << error.message() << std::endl;
            return;
        }

        // Extract the newline-delimited message from the buffer.
        std::string line;
        std::istream is(&_buffer);
        std::getline(is, line);

        if (!line.empty())
        {
            std::cout << "Received: " << line << "\n";
        }

        start_read();
    }

    tcp::socket _socket;
    std::string _daymessage;
    boost::asio::streambuf _buffer;
};

int main()
{
    std::cout << "Client is running!" << std::endl;

    try
    {
        boost::asio::io_service io_service;

        tcp::resolver resolver(io_service);

        tcp::resolver::query query("127.0.0.1", "7171");

        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

        auto connection = Connection::create(io_service);

        connection->connect(endpoint_iterator);

        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

obs:当我使用public ctor并使用make_shared实例化时,我得到了一个“tr1 :: bad_weak_ptr”。私人ctor和静态成员func工作得很好。

1 个答案:

答案 0 :(得分:0)

您提供的代码存在许多问题:

  • 在您的服务器连接类中,您使用的是原始function showlasttenbuilds(){ document.getElementById("lasttenBuildsTable").style.display="Block"; } 指针 而不是使用this。这导致了 当您的资源退出时,获取shared_from_this的操作 范围。
  • 代码正在使用cancelled和一个大小为126的缓冲区 猜测操作不会完成,直到你收到至少那个 很多字节。请改用async_read。由于缺少任何协议或预定义的字节序列,我修改了代码 发送&#39; \ n&#39;作为分隔符。
  • 永远不要忽略收到的错误代码。

修改后的服务器代码:

async_read_until

修改后的客户端代码:

class tcp_connection: public boost::enable_shared_from_this<tcp_connection>
{
public:
    typedef boost::shared_ptr<tcp_connection> pointer;

    static pointer create(boost::asio::io_service& io_service)
    {
        return pointer(new tcp_connection(io_service));
    }

    tcp::socket& socket()
    {
        return socket_;
    }

    // Call boost::asio::async_write() to serve the data to the client.
    // We are using boost::asio::async_write(),
    // rather than ip::tcp::socket::async_write_some(),
    // to ensure that the entire block of data is sent.

    void start()
    {
        // The data to be sent is stored in the class member m_message
        // as we need to keep the data valid
        // until the asynchronous operation is complete.

        m_message = make_daytime_string();

        // When initiating the asynchronous operation,
        // and if using boost::bind(),
        // we must specify only the arguments
        // that match the handler's parameter list.
        // In this code, both of the argument placeholders
        // (boost::asio::placeholders::error
        // and boost::asio::placeholders::bytes_transferred)
        // could potentially have been removed,
        // since they are not being used in handle_write().

        std::cout << socket_.remote_endpoint().address().to_string() << ":" << socket_.remote_endpoint().port() << " connected!" << std::endl;

        boost::asio::async_write(socket_, boost::asio::buffer(m_message),
                                 boost::bind(&tcp_connection::handle_write, shared_from_this()));
        boost::asio::async_read_until(socket_,
            _buffer,
            '\n',
            boost::bind(&tcp_connection::handle_receive,
              shared_from_this(),
              boost::asio::placeholders::error));
    }


private:
    tcp_connection(boost::asio::io_service& io_service)
    : socket_(io_service)
    {
    }
    // handle_write() is responsible for any further actions
    // for this client connection.

    void handle_write() // call back.. when it finishes sending, we come here
    {
        std::cout << "Client has received the messaged. " << std::endl;
    }

    void handle_receive(const boost::system::error_code& ErrorCode)
    {
        std::cout << "You received the following message from the server: "<< std::endl;
        if (ErrorCode) {
          std::cout << "Error occured: " << ErrorCode.message() << std::endl;
          return;
        }
        std::string line;
        std::istream is(&_buffer);
        std::getline(is, line);
        std::cout << line << std::endl;

    }

    tcp::socket socket_;
    std::string m_message;
    boost::asio::streambuf _buffer;
};