简体   繁体   English

async_read不读取从telnet发送的数据

[英]async_read doesn't read data sent from telnet

This is my current code for the server. 这是我当前的服务器代码。 I connect to the server using telnet. 我使用telnet连接到服务器。

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

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

class Connection
{
public:
    Connection(boost::asio::io_service& io_service) : socket_(io_service)
    {

    }

    void start()
    {
        AsyncRead();
    }

    tcp::socket& socket()
    {
        return socket_;
    }
private:
    void AsyncRead()
    {
        boost::asio::async_read(socket_, boost::asio::buffer(data_, max_length),
                                [this](boost::system::error_code ec, std::size_t length)
                                {
                                    if (!ec)
                                    {
                                        std::cout << data_ << std::endl;
                                    }
                                });
    }

    tcp::socket socket_;
    enum { max_length = 1024 };
    char data_[max_length];
};


class server
{
public:
    server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
        acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
    {
        start_accept();
    }

private:
    void start_accept()
    {
        Connection* connection = new Connection(io_service_);
        acceptor_.async_accept(connection->socket(), [this, connection](boost::system::error_code ec)
                               {
                                   //std::cout << ec.message() << std::endl;
                                   if(!ec)
                                   {
                                       std::cout << "Connected." << std::endl;
                                       connection->start();
                                   }
                                   else
                                   {
                                       delete connection;
                                   }

                                   start_accept();
                               });
    }

    boost::asio::io_service& io_service_;
    tcp::acceptor acceptor_;
};

int main(int argc, char* argv[])
{
    try
    {
        boost::asio::io_service io_service;

        server s(io_service, 7899);

        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}

Instead of async_read I use async_read_some I can get the first message sent from telnet and thats it. 取而代之的async_read我用async_read_some我可以从telnet和多数民众赞成它发送的第一条消息。

Any suggestions on what I am doing wrong ? 对我做错了什么建议?
Thanks. 谢谢。

async_read will read the number of bytes specified in the length parameter. async_read将读取length参数中指定的字节数。 You aren't seeing the first message because async_read is still waiting to read max_length bytes. 您没有看到第一条消息,因为async_read仍在等待读取max_length个字节。 This question discusses similar behaviour 这个问题讨论了类似的行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM