gethostname 函数始终返回 nullptr

发布于 2025-01-21 03:08:14 字数 337 浏览 2 评论 0原文

我正在尝试在Windows中解决C中的主机名。无论我输入什么地址,它总是返回nullptr。我在Python解释器中尝试了相同的功能,并且它可以正常运行,因此此功能之外没有错,这会导致其无法解析DNS。甚至“ Localhost”也失败了。尝试访问主机变量中的任何内容会导致崩溃。

    struct hostent* host = gethostbyname("google.com");

    if (host) {
        printf("Yes\n");
    }
    else {
        printf("No\n"); // Always gets here
    }

I am attempting to resolve a hostname in C in windows. No matter what I enter in as the address it always returns a nullptr. I have tried the same function in a python interpreter and it works fine, so there is nothing wrong outside of this function that would cause it to be unable to resolve a DNS. Even "localhost" fails aswell. Attempting to access anything in the host variables causes a crash.

    struct hostent* host = gethostbyname("google.com");

    if (host) {
        printf("Yes\n");
    }
    else {
        printf("No\n"); // Always gets here
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文