async_send数据未发送

时间:2017-01-14 13:38:03

标签: c++ sockets boost boost-asio

[免责声明]我是新手来提升。

调查boost :: asio并尝试使用以下功能创建一个简单的异步TCP服务器:

  1. 侦听端口13上的连接
  2. 连接后,接收数据
  3. 如果收到数据==时间,则返回当前日期时间,否则返回预定义字符串(“请求其他内容”)
  4. 问题: 虽然,我接受连接并接收数据,但是当使用async_send传输数据时,虽然我没有收到任何错误并且bytes_transferred的值是正确的,但我在客户端收到空数据。

    如果我尝试从handle_accept(而不是handle_read)中传输数据,这可以正常工作。

    实施: 我参与了增强的asio教程here: 实例化一个tcp_server对象,它基本上启动接受器并开始监听。如下图所示:

    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;
    }
    

    和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";
        }
        ...
    }
    

    接受连接后,我正在处理它,如下所示:

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

    以下是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));*/
    }
    

    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));
        }
    }
    

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

    请注意以下handle_write,这是非常奇怪的事情):

    • 没有错误
    • bytes_transferred变量具有正确的值
    • outputBuffer_具有正确的值(在handle_read中设置)

    然而,在客户端(Packet Sender)收到的包是空的(就数据而言)。

    完整代码已共享here

3 个答案:

答案 0 :(得分:1)

完成测试程序(c ++ 14)。请注意响应接收时异步缓冲的处理 - 可能正在发送一个发送。

#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();

}

答案 1 :(得分:1)

此代码中存在多个问题。其中一些人可能对您的问题负责:

  • TCP没有数据包的定义,因此无法保证您在handle_read中一次性收到time。你需要一个statemachine并尊重bytes_transferred信息。如果您只收到了部分消息,则需要以正确的偏移量继续。或者您可以使用asio实用程序函数,例如读取一个字节长度或读取一行。
  • 除了最后一点,您不应该真正将收到的数据与strcmp进行比较。只有当远程通过连接发送空终结符时,这才有效 - 是吗?
  • 您不会检查是否发生错误,尽管这可能会在其他错误中显现出来。
  • 如果在shart时间跨度内收到多个数据片段,则可能会发出多个并发异步写入。这在asio中无效。
  • 更重要的是,您在发送过程中改变发送缓冲区(outputBuffer_)。这几乎会导致未定义的行为。 asio可能会尝试编写一段不再有效的内存。

答案 2 :(得分:0)

我已经在问题中提供的评论的集体帮助下解决了这个问题。我遇到的行为是因为async_read的功能。更具体地说,它在boost asio documentation中读到:

  

此函数用于异步读取一定数量的字节   来自流的数据。函数调用总是立即返回。   异步操作将继续,直到下列之一   条件是真的:

     
      
  • 提供的缓冲区已满。也就是说,传输的字节数等于缓冲区大小的总和。
  •   
  • 发生错误。
  •   

我用来读取输入的inputBuffer_是一个128字符数组。我正在使用的客户端只传输实际数据(没有填充),因此在客户端关闭连接(或传输了128个字节的数据)之前,async_read不会返回。当客户端关闭连接时,无法发送回请求的数据。这也是它与@Arunmu的简单python tcp客户端合作的原因(因为他总是发送128字节的数据)。

要解决这些问题,我做了以下更改(提供完整的工作代码here以供参考):

  • tcp_connection::start中:我现在使用async_read_until来读取传入的数据(并使用\n作为分隔符)。输入存储在boost::asio::streambuf中。一旦找到分隔符或发生错误,保证async_read返回。因此,没有机会同时发布多个async_write
  • handle_read中:我已经包含了错误检查,这使得调试变得更加简单。