简体   繁体   English

未发送async_send数据

[英]async_send data not sent

[disclaimer] I am new to boost. [免责声明]我是新人。

Looking into boost::asio and tried to create a simple asynchronous TCP server with the following functionality: 研究boost :: asio并尝试创建具有以下功能的简单异步TCP服务器:

  1. Listen for connections on port 13 监听端口13上的连接
  2. When connected, receive data 连接后,接收数据
  3. If data received == time, then return current datetime, else return a predefined string ("Something else was requested") 如果收到的数据==时间,则返回当前日期时间,否则返回预定义的字符串(“请求其他内容”)

The problem: Although, I accept the connection and receive the data, when transmitting data using async_send, although I receive no error and the value of bytes_transferred is correct, I receive empty data on the client side. 问题:虽然我接受连接并接收数据,但是当使用async_send传输数据时,尽管我没有收到错误并且bytes_transferred的值正确,但是我在客户端接收了空数据。

If I try to transmit the data from within handle_accept (instead of handle_read), this works fine. 如果我尝试从handle_accept(而不是handle_read)中传输数据,则可以正常工作。

The implementation: I worked on the boost asio tutorial found here : Instantiate a tcp_server object, that basically initiates the acceptor and starts listening. 实现:我在这里的boost asio教程上工作:实例化一个tcp_server对象,该对象基本上会启动接受器并开始监听。 as shown below: 如下所示:

int main()
{
    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;
}

and in tcp_server: 并在tcp_server中:

class tcp_server
{
public:
    tcp_server(boost::asio::io_service& io_service)
        : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
    {
        start_accept();
    }

private:
    void start_accept()
    {
        using std::cout;
        tcp_connection::pointer new_connection =
            tcp_connection::create(acceptor_.get_io_service());

        acceptor_.async_accept(new_connection->socket(),
            boost::bind(&tcp_server::handle_accept, this, new_connection,
                boost::asio::placeholders::error));
        cout << "Done";
    }
    ...
}

Once a connection is accepted, I am handling it as shown below: 接受连接后,我将按如下所示进行处理:

void handle_accept(tcp_connection::pointer new_connection,
        const boost::system::error_code& error)
{
    if (!error)
    {
        new_connection->start();
    }

    start_accept();
}

Below is the tcp_connection::start() method: 下面是tcp_connection::start()方法:

void start()
{

    boost::asio::async_read(socket_, boost::asio::buffer(inputBuffer_),
        boost::bind(&tcp_connection::handle_read, shared_from_this(),
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

    /* the snippet below works here - but not in handle_read 
    outputBuffer_ = make_daytime_string();

    boost::asio::async_write(socket_, boost::asio::buffer(outputBuffer_),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));*/
}

and in handle_read : 并在handle_read

void handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
    outputBuffer_ = make_daytime_string();
    if (strcmp(inputBuffer_, "time"))
    {
        /*this does not work - correct bytes_transferred but nothing shown on receiving end */
        boost::asio::async_write(socket_, boost::asio::buffer(outputBuffer_),
            boost::bind(&tcp_connection::handle_write, shared_from_this(),
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred)); 
    }
    else
    {
        outputBuffer_ = "Something else was requested";//, 128);
        boost::asio::async_write(socket_, boost::asio::buffer(outputBuffer_),
            boost::bind(&tcp_connection::handle_write, shared_from_this(),
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));
    }
}

The handle_write is shown below: handle_write如下所示:

void handle_write(const boost::system::error_code& error,
    size_t bytes_transferred)
{
    if (!error)
    {
        std::cout << "Bytes transferred: " << bytes_transferred;
        std::cout << "Message sent: " << outputBuffer_;
    }
    else
    {
        std::cout << "Error in writing: " << error.message();
    }
}

Note the following regarding handle_write ( and this is the really strange thing ): 请注意以下有关handle_write这确实很奇怪 ):

  • There is no error 没有错误
  • The bytes_transferred variable has the correct value bytes_transferred变量的值正确
  • outputBuffer_ has the correct value (as set in handle_read) outputBuffer_具有正确的值(在handle_read中设置)

Nevertheless, the package received at the client side ( Packet Sender ) is empty (as far as data is concerned). 但是,在客户端( Packet Sender )接收的是空的(就数据而言)。

The complete code is shared here . 完整的代码在这里共享。

Complete test program (c++14). 完整的测试程序(c ++ 14)。 Note the handling of asynchronous buffering when responding to a receive - there may be a send already in progress. 注意响应接收时的异步缓冲处理-可能已经在进行发送。

#include <boost/asio.hpp>
#include <thread>
#include <future>
#include <vector>
#include <array>
#include <memory>
#include <mutex>
#include <condition_variable>
#include <iterator>
#include <iostream>

namespace asio = boost::asio;

asio::io_service        server_service;
asio::io_service::work  server_work{server_service};

bool listening = false;
std::condition_variable cv_listening;
std::mutex              management_mutex;

auto const shared_query = asio::ip::tcp::resolver::query(asio::ip::tcp::v4(), "localhost", "8082");

void client()
try
{
    asio::io_service      client_service;
    asio::ip::tcp::socket socket(client_service);

    auto lock = std::unique_lock<std::mutex>(management_mutex);
    cv_listening.wait(lock, [] { return listening; });
    lock.unlock();

    asio::ip::tcp::resolver resolver(client_service);
    asio::connect(socket, resolver.resolve(shared_query));
    auto s = std::string("time\ntime\ntime\n");
    asio::write(socket, asio::buffer(s));
    socket.shutdown(asio::ip::tcp::socket::shutdown_send);

    asio::streambuf sb;
    boost::system::error_code sink;
    asio::read(socket, sb, sink);
    std::cout << std::addressof(sb);
    socket.close();
    server_service.stop();
}
catch(const boost::system::system_error& se)
{
    std::cerr << "client: " << se.code().message() << std::endl;
}

struct connection
    : std::enable_shared_from_this<connection>
{
    connection(asio::io_service& ios)
        : strand_(ios)
    {

    }

    void run()
    {
        asio::async_read_until(socket_, buffer_, "\n",
                               strand_.wrap([self = shared_from_this()](auto const&ec, auto size)
        {
            if (size == 0 )
            {
                // error condition
                boost::system::error_code sink;
                self->socket_.shutdown(asio::ip::tcp::socket::shutdown_receive, sink);
            }
            else {
                self->buffer_.commit(size);
                std::istream is(std::addressof(self->buffer_));
                std::string str;
                while (std::getline(is, str))
                {
                    if (str == "time") {
                        self->queue_send("eight o clock");
                    }
                }
                self->run();
            }
        }));
    }

    void queue_send(std::string s)
    {
        assert(strand_.running_in_this_thread());
        s += '\n';
        send_buffers_pending_.push_back(std::move(s));
        nudge_send();
    }

    void nudge_send()
    {
        assert(strand_.running_in_this_thread());
        if (send_buffers_sending_.empty() and not send_buffers_pending_.empty())
        {
            std::swap(send_buffers_pending_, send_buffers_sending_);
            std::vector<asio::const_buffers_1> send_buffers;
            send_buffers.reserve(send_buffers_sending_.size());
            std::transform(send_buffers_sending_.begin(), send_buffers_sending_.end(),
                           std::back_inserter(send_buffers),
            [](auto&& str) {
                return asio::buffer(str);
            });
            asio::async_write(socket_, send_buffers,
                              strand_.wrap([self = shared_from_this()](auto const& ec, auto size)
            {
                // should check for errors here...
                self->send_buffers_sending_.clear();
                self->nudge_send();
            }));
        }
    }

    asio::io_service::strand strand_;
    asio::ip::tcp::socket    socket_{strand_.get_io_service()};
    asio::streambuf          buffer_;

    std::vector<std::string> send_buffers_pending_;
    std::vector<std::string> send_buffers_sending_;
};

void begin_accepting(asio::ip::tcp::acceptor& acceptor)
{
    auto candidate = std::make_shared<connection>(acceptor.get_io_service());
    acceptor.async_accept(candidate->socket_, [candidate, &acceptor](auto const& ec)
    {
        if (not ec) {
            candidate->run();
            begin_accepting(acceptor);
        }
    });
}

void server()
try
{
    asio::ip::tcp::acceptor acceptor(server_service);
    asio::ip::tcp::resolver resolver(server_service);

    auto first = resolver.resolve(shared_query);
    acceptor.open(first->endpoint().protocol());
    acceptor.bind(first->endpoint());

    acceptor.listen();

    begin_accepting(acceptor);

    auto lock = std::unique_lock<std::mutex>(management_mutex);
    listening = true;
    lock.unlock();
    cv_listening.notify_all();


    server_service.run();

}
catch(const boost::system::system_error& se)
{
    std::cerr << "server: " << se.code().message() << std::endl;
}

int main()
{

    using future_type = std::future<void>;

    auto stuff = std::array<future_type, 2> {{std::async(std::launch::async, client),
                                                 std::async(std::launch::async, server)}};

    for (auto& f : stuff) f.wait();

}

There are multiple issues in this code. 此代码中存在多个问题。 Some of them may be responsible for your problem: 其中一些可能与您的问题有关:

  • TCP has no definition of packets, so there's no guarantee that you will ever receive time at once in handle_read. TCP没有数据包的定义,因此无法保证您将在handle_read中一次收到time You need a statemachine for that and to respect the bytes_transferred info. 您需要一个状态机并尊重bytes_transferred信息。 If you only have received a part of the message you need to continue at the correct offset. 如果仅收到部分消息,则需要以正确的偏移量继续。 Or you can use asio utility functions, like reading exactly a length of bytes or reading a line. 或者,您可以使用asio实用程序功能,例如精确读取字节长度或读取一行。
  • In addition the last point, you shouldn't really compare the received data with strcmp . 除了最后一点,您不应该真正将接收到的数据与strcmp进行比较。 That will only work if the remote also sends a null terminator over the connection - does it? 这仅在远程也通过连接发送了空终止符的情况下起作用吗?
  • You don't check whether an error happend, although that might manifest itself in other errors. 您无需检查是否发生错误,尽管这可能会在其他错误中体现出来。
  • You are possibly issueing multiple concurrent async writes if you receive multiple data fragments in a shart timespan. 如果您在短时间内收到多个数据片段,则可能发出多个并发异步写入。 This is not valid in asio. 这在asio中无效。
  • More important, you mutate the send buffer ( outputBuffer_ ) while the send is in progress. 更重要的是,您可以在发送过程中outputBuffer_发送缓冲区( outputBuffer_ )。 This will pretty much lead to undefined behavior. 这几乎会导致不确定的行为。 asio might try to write a piece of memory which is no longer valid. asio可能会尝试写入不再有效的内存。

I have solved the problem with the collective help of the comments provided in the question. 我已经在问题中提供的评论的集体帮助下解决了这个问题。 The behavior that I was experiencing was because of the functionality of async_read . 我遇到的行为是由于async_read的功能。 More specifically in the boost asio documentation it reads: 更具体地说,在boost asio文档中显示为:

This function is used to asynchronously read a certain number of bytes of data from a stream. 此函数用于从流中异步读取一定数量的数据字节。 The function call always returns immediately. 函数调用总是立即返回。 The asynchronous operation will continue until one of the following conditions is true: 异步操作将继续,直到满足以下条件之一:

  • The supplied buffers are full. 提供的缓冲区已满。 That is, the bytes transferred is equal to the sum of the buffer sizes. 即,传输的字节等于缓冲区大小的总和。
  • An error occurred. 发生错误。

The inputBuffer_ I was using to read the input, was a 128 char array. 我用来读取输入的inputBuffer_是一个128个字符的数组。 The client I was using, would only transfer the real data (without padding), and therefore the async_read would not return until the connection was closed by the client (or 128 bytes of data were transferred). 我使用的客户端将仅传输实际数据(不带填充),因此async_read在客户端关闭连接(或传输128字节数据)之前不会返回。 When the connection was closed by the client there was no way to send back the requested data. 当客户端关闭连接时,无法发送回所请求的数据。 This is also the reason that it was working with @Arunmu's simple python tcp client (because he was sending 128 bytes of data always). 这也是它与@Arunmu的简单python tcp客户端一起使用的原因(因为他始终发送128字节的数据)。

To fix the issues, I made the following changes (the full working code is supplied here for reference): 为了解决这些问题,我进行了以下更改( 此处提供了完整的工作代码供参考):

  • In tcp_connection::start : I am now using async_read_until to read the incoming data (and use \\n as a delimiter). tcp_connection::start :我现在正在使用async_read_until读取传入的数据(并使用\\n作为分隔符)。 The input is stored in a boost::asio::streambuf . 输入存储在boost::asio::streambuf async_read is guaranteed to return once the delimiter has been found, or an error has occurred. 找到分隔符或发生错误后,保证async_read返回。 So there is no chance to issue multiple async_write concurrently. 因此,没有机会同时发出多个async_write
  • In handle_read : I have included error checking, which made it much simpler to debug. handle_read :我包括了错误检查,这使调试变得更加简单。

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

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