简体   繁体   English

inet_ntop() 在此范围内未清除

[英]inet_ntop() was not decleared in this scope

I am building a TCP server using C: I included the library where the inet_ntop() function is (WS2tcpip.h), yet my compiler returns with the following error:我正在使用 C 构建 TCP 服务器:我包含了 inet_ntop() 函数所在的库 (WS2tcpip.h),但我的编译器返回以下错误:

'inet_ntop' was not declared in this scope 'inet_ntop' 未在此范围内声明

I have no idea why;我不知道为什么; even my IDE doesn't notice anything wrong (I use vscode as my IDE because it can detect errors in real-time without compiling every time) yet when I compile it gcc returns the error I stated previously.即使我的 IDE 也没有发现任何错误(我使用 vscode 作为我的 IDE,因为它可以实时检测错误而无需每次都编译)但是当我编译它时,gcc 返回我之前提到的错误。

What I have included and defined:我所包含和定义的内容:

#include <stdio.h>
#include <WS2tcpip.h>

#define WIN32_LEAN_AND_MEAN

#pragma comment (lib, "ws2_32.lib")

The faulty line of code:错误的代码行:

if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXHOST, 0) == 0) {
        printf("\n[CONNECTION]: %c connesso sulla porta %c", host, service);
    } else {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        printf("\n[CONNECTION]: %c connesso sulla porta %c", host, ntohs(client.sin_port));
    }

Any helpful tip is appreciated, thanks!任何有用的提示表示赞赏,谢谢!

Are you working on Windows?你在 Windows 上工作吗? If not, try including arpa/inet.h如果没有,请尝试包含 arpa/inet.h

Are you using VSCode?你在使用 VSCode 吗? or manual compilers?或手动编译器?

I have pretty same error with my code.我的代码有同样的错误。 Im using C++ in my code but I get error with these: 'getnameinfo' was not declared in this scope 'inet_ntop' was not declared in this scope我在我的代码中使用 C++,但我收到以下错误:'getnameinfo' 未在此范围内声明 'inet_ntop' 未在此范围内声明

I use VS Code.我使用 VS 代码。 What I realize is, when I copy paste same code to VS, it works!我意识到的是,当我将相同的代码复制粘贴到 VS 时,它起作用了! I think its something with compiler problem.我认为它与编译器问题有关。

Still can't compile my code in VSCode but in VS it works.仍然无法在 VSCode 中编译我的代码,但在 VS 中它可以工作。

Your compiler is most likely outdated and contains an outdated version of the <ws2tcpip.h> header which does not contain the inet_ntop function implementation.您的编译器很可能已经过时,并且包含一个过时版本的<ws2tcpip.h>标头,其中不包含inet_ntop函数实现。

I've had the same issue and resolved it by downloading MinGW-w64 9.0.0 with GCC 11.1.1 from here .我遇到了同样的问题,并通过从这里下载MinGW-w64 9.0.0 with GCC 11.1.1解决了它。

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

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