boost::asio::ip::tcp::resolver::resolve() 永远阻塞

发布于 2024-07-15 19:46:40 字数 1590 浏览 13 评论 0原文

我正在尝试创建类似于 的内容这段代码可以在 boost.asio 示例中找到。

socket.h:

class some_class {
private:
    ...
        boost::asio::io_service io_service;
public:
        some_class() {
             /* This stuff isn't used in the example...
               ...but it doesn't change anything... */
             io_service.run();
        }
};

socket.cpp:

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

bool some_class::connect(char* host, char* port) 
{
    printf("Resolving hostname...\n");

    /* Resolve hostname. */
    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), host, port);
    tcp::resolver::iterator iterator = resolver.resolve(query);

    printf("Connecting to %s:%s... ", host, port);

    /* Connect to resolved hosts. */
    sock->connect(*iterator);

    return true;
}

g++ 构建此代码没有任何错误,但代码从未通过resolver.resolve() 调用。
我尝试过主机使用“127.0.0.1”和“localhost”,端口使用“80”。 (不认为这应该重要,但 apache2 已启动并正在运行)

当我 ctrl+c 退出我的应用程序时,它显然终止了,但它确实在它之前输出了“连接到字符串”。

我打算自己构建示例,看看是否出现同样的问题,并且肯定会在此处发布结果。 有谁遇到过这个问题或者知道什么可能导致这种行为?

编辑:
该示例运行得很好...我想我需要进行一些调试。

第二次编辑:
我不明白,唯一可能不同的是主机/端口。
示例使用 char* argv[],我正在使用:

char host[] = "localhost";
char port[] = "80";

第三次编辑:
它确实似乎在连接时阻塞,忘记了 fflush(stdout)。 那么一定是socket的问题。 要做更多测试。

第四次编辑:
我傻了,根本就没有阻塞! 我只是太依赖控制台输出了..

I'm trying to create something similar as this code found at the boost.asio examples.

socket.h:

class some_class {
private:
    ...
        boost::asio::io_service io_service;
public:
        some_class() {
             /* This stuff isn't used in the example...
               ...but it doesn't change anything... */
             io_service.run();
        }
};

socket.cpp:

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

bool some_class::connect(char* host, char* port) 
{
    printf("Resolving hostname...\n");

    /* Resolve hostname. */
    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), host, port);
    tcp::resolver::iterator iterator = resolver.resolve(query);

    printf("Connecting to %s:%s... ", host, port);

    /* Connect to resolved hosts. */
    sock->connect(*iterator);

    return true;
}

g++ builds this without any errors, but the code never makes it past the resolver.resolve() call.

I've tried both "127.0.0.1" and "localhost" for host and "80" for port. (don't think it should matter, but apache2 is up and running)

When I ctrl+c out of my application, it obviously terminates but it does output the "Connecting to string" just before it does.

I am planning on building the example myself and seeing if the same problem occurs, and will definitely post the results here. Has anyone encountered this issue or knows what could possibly cause this behavior?

edit:
The example runs just fine... I have some debugging to do I suppose.

second edit:
I don't get it, the only thing that could be different is host/port.
Example uses char* argv[] and I'm using:

char host[] = "localhost";
char port[] = "80";

third edit:
it indeed seems to be blocking at connect, forgot to fflush(stdout). then it has to be a problem with the socket. going to do some more testing.

fourth edit:
stupid me, it wasn't blocking at all! I was just relying too much on console output..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-07-22 19:46:41

它可能在 printf 之后的连接调用上被阻塞。

stdout 默认情况下是行缓冲的,并且由于 printf 字符串末尾没有 \n,因此您将看不到它的输出。 当您终止程序时,缓冲区将被刷新,这就是您看到该消息的原因。

It is probably blocking on the call to connect, after the printf.

stdout is line buffered by default, and since you do not have a \n at the end of your printf string, you will not see its output. When you kill the program, the buffer is being flushed, which is why you see the message then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文