【问题标题】:'inet_ntop' was not decleared in this scope'inet_ntop' 未在此范围内声明
【发布时间】:2021-09-20 16:07:38
【问题描述】:
if (getnameinfo((sockaddr *) & client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
    std::cout << host << " Connected on port " << service << std::endl;
}
else
{ 
    inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
    std::cout << host << " Connected on port " << ntohs(client.sin_port) << std::endl;
}

我希望在端口上连接“主机”,但我得到了错误:

'inet_ntop' 未在此范围内声明

我尝试添加更多 #include 语句,但没有成功。我想我收到错误是因为我可能有过时的东西?

编辑:我需要做一个minimal reproducible example,但我的英语很差,所以我需要一年的时间来学习/阅读,所以我要去另一个地方寻求帮助: -)

【问题讨论】:

  • 您尝试过的包含文件中有ws2tcpip.h 吗?
  • 这是我拥有的“包含”文件#include &lt;w32api.h&gt; #include &lt;SdkDdkver.h&gt; #include &lt;iostream&gt; #include &lt;WS2tcpip.h&gt; #include &lt;winsock2.h&gt; #include &lt;windows.h&gt; #pragma comment(lib, "ws2_32.lib") @WhozCraig
  • 专业提示:您知道吗,如果您在 Google 上搜索“ windows”,您很可能会立即获得指向 Microsoft 库函数官方文档的链接,作为第一次点击,包含您需要知道的所有内容,包括您需要包含哪些头文件?
  • 好吧,我做到了,它说的是我从一开始就拥有的 ws2tcpip.h @SamVarshavchik
  • 在这种情况下,您的下一步将是edit 您的问题并显示minimal reproducible example,这意味着其他所有人都可以剪切/粘贴您的问题中显示的内容question ,然后编译并重现您的错误(现在不是这种情况)。请参阅 Stackoverflow 的 help center 中的“How to Ask 问题”。

标签: c++ windows tcp


【解决方案1】:

确保您的代码中有#include &lt;ws2tcpip.h&gt;

但还要注意 inet_ntop() 在 Windows Vista 中已添加到 WinSock,因此请确保您的项目配置为面向 Vista 或更高版本,因为 ws2tcpip.h 仅在 NTDDI_VERSION &gt;= NTDDI_VISTA (0x06000000) 时声明 inet_ntop()(请参阅Using the Windows HeadersUpdate WINVER and _WIN32_WINNT)。

如果在项目/编译器级别定位 Vista+ 不是一个选项(即,您也需要以 XP 为目标),那么您可以在运行时通过 GetProcAddress() 导入 inet_ntop()

【讨论】:

    猜你喜欢
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2019-02-17
    • 2021-01-07
    • 2016-10-27
    • 2016-06-03
    相关资源
    最近更新 更多