简体   繁体   English

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

[英]boost::asio::ip::tcp::resolver::resolve() blocks forever

I'm trying to create something similar as this code found at the boost.asio examples.我正在尝试创建与 boost.asio 示例中的此代码类似的内容。

socket.h:套接字.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:套接字.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. g++ 在没有任何错误的情况下构建了它,但代码永远不会通过 resolver.resolve() 调用。
I've tried both "127.0.0.1" and "localhost" for host and "80" for port.我已经为主机尝试了“127.0.0.1”和“localhost”,为端口尝试了“80”。 (don't think it should matter, but apache2 is up and running) (不认为应该有关系,但 apache2 已启动并运行)

When I ctrl+c out of my application, it obviously terminates but it does output the "Connecting to string" just before it does.当我 ctrl+c 退出我的应用程序时,它显然会终止,但它会在 output 之前执行“连接到字符串”。

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* argv[] 我正在使用:

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

third edit:第三次编辑:
it indeed seems to be blocking at connect, forgot to fflush(stdout).它确实似乎在连接时阻塞,忘记了 fflush(stdout)。 then it has to be a problem with the socket.那么肯定是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..我只是过于依赖控制台 output..

It is probably blocking on the call to connect, after the printf.在 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.默认情况下,stdout 是行缓冲的,并且由于 printf 字符串的末尾没有 \n,因此您将看不到它的 output。 When you kill the program, the buffer is being flushed, which is why you see the message then.当您终止程序时,缓冲区正在被刷新,这就是您看到该消息的原因。

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

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