io_service::run() 永远不会返回

io_service::run() never returns

本文关键字:永远 返回 service run io      更新时间:2023-10-16

UPDATE_2我最终使用

if (socket.available())

如果套接字上有数据,我会读取它,如果没有,我会跳过。

更新我有一个问题。io_service::run(( 永远不会返回以下代码片段:

客户端:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
using namespace std;
using boost::asio::ip::tcp;
void handler(
    const boost::system::error_code &error,
    size_t bytes_transferred
)
{
    cout << "Handler called" << endl;
}
int main()
{
    //establishing connection
    boost::asio::io_service io_service;
    tcp::socket socket(io_service);
    tcp::resolver resolver(io_service);
    tcp::resolver::query query("localhost", "17073");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    boost::asio::connect(socket, endpoint_iterator);
    //connection established
    boost::array<char, 128> buf;
    socket.async_read_some(boost::asio::buffer(buf), boost::bind(handler, _1, _2));
    //async_read_some returns
    io_service.run();       //I suppose that handler_for_async_response is called if:
                            //1) EOF was read (no data read twice) by async_read_some
                            //2) something was in fact read
                            //is this right?
    //execution does not get here
    return 0;
}

服务器端(只接受连接并挂在那里什么都不做(:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
using namespace std;
using boost::asio::ip::tcp;
int main()
{
    //establishing connection
    boost::asio::io_service io_service;
    tcp::socket             socket(io_service);
    tcp::acceptor           acceptor(io_service, tcp::endpoint(tcp::v4(), 17073));
    acceptor.accept(socket);
    std::cout << "connection has been established!" << std::endl;
    //connection established
    while (true)
    {
        //do nothing
    }
    return 0;
}

我想做的是查看套接字上是否有任何数据。如果是 - 然后阅读它,如果不是 - 然后继续。

PS我打算从套接字读取和写入套接字。

无论如何,我是 boost::asio 的新手,我希望有人能告诉我我做错了什么。谢谢!

更新 用代码回答更新的问题:


客户端正在等待 128 字节或 EOF。这些都没有来,因为服务器不发送任何内容(但不关闭连接(。

任一更改

  1. 客户,例如

    • 不要等待实际数据到达

      boost::array<char, 0> buf;
      
    • 或者在deadline_timer(左右(后使其socket.cancel()

  2. 服务器,例如用真正的套接字写入替换无限循环

下面是一个示例

住在科里鲁

#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
using boost::asio::ip::tcp;
using namespace std;
#ifdef SERVER
    int main()
    {
        //establishing connection
        boost::asio::io_service io_service;
        tcp::socket             socket(io_service);
        tcp::acceptor           acceptor(io_service, tcp::endpoint(tcp::v4(), 17073));
        acceptor.accept(socket);
        std::cout << "connection has been established!" << std::endl;
        //connection established
        socket.send(boost::asio::buffer(std::string("Hello worldn")));
    }
#else
    void handler(
        boost::array<char, 128> const& buf,
        const boost::system::error_code &error,
        size_t bytes_transferred
    )
    {
        cout << "Handler called (" << error.message() << "): "  << endl;
        cout.write(buf.data(), bytes_transferred);
    }
    int main()
    {
        //establishing connection
        boost::asio::io_service io_service;
        tcp::socket socket(io_service);
        tcp::resolver resolver(io_service);
        tcp::resolver::query query("127.0.0.1", "17073");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        boost::asio::connect(socket, endpoint_iterator);
        //connection established
        boost::array<char, 128> buf;
        socket.async_read_some(boost::asio::buffer(buf), boost::bind(handler, boost::ref(buf), _1, _2));
        //async_read_some returns
        io_service.run();       //I suppose that handler_for_async_response is called if:
                                //1) EOF was read (no data read twice) by async_read_some
                                //2) something was in fact read
                                //is this right?
        //execution does not get here
        return 0;
    }
#endif

指纹:

//(server)
connection has been established!
//(client)
Handler called (Success): 

世界您好

您必须对同步或异步编程下定决心。

在这种情况下,我认为您可能真的在寻找tcp::istream


运行在任务用完时返回:

  • http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/reference/io_service/run/overload1.html

run()功能将一直阻止,直到所有工作完成并且没有更多处理程序要调度,或者直到io_service停止为止。

多个线程可以调用 run() 函数来设置线程池,io_service可以从中执行处理程序。在池中等待的所有线程都是等效的,io_service可以选择其中任何一个来调用处理程序。

run() 函数正常退出意味着io_service对象已停止(stop(( 函数返回 true(。对run()run_one()poll()poll_one()的后续调用将立即返回,除非事先调用reset()

因此,您在服务上还有其他待处理的工作(io_service::工作?检查一下。将您的代码设为 SSCCE,我们将能够看到更多。