C++ 'inet_addr':使用 inet_pton() 或 InetPton() 代替或定义 _WINSOCK_DEPRECATED_NO_WARNINGS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36683785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS
提问by The Count
I'm using Visual Studio 2015 and attempting to compile code that has worked before I updated from VS 2013.
我正在使用 Visual Studio 2015 并尝试编译在我从 VS 2013 更新之前已经工作的代码。
'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS
'inet_addr':使用 inet_pton() 或 InetPton() 代替或定义 _WINSOCK_DEPRECATED_NO_WARNINGS
partner.sin_addr.s_addr = inet_addr(ip.c_str());
I attempted to use the functions mentioned but they were undefined. I attempted to define the macro in many different spots but nothing happened. Another thread said that I should include Ws2tcpip.h instead of WinSock2 & add Ws2_32.lib. I already have the library added, and when I used the include nothing happened. What is going on?!
我尝试使用提到的函数,但它们未定义。我试图在许多不同的地方定义宏,但什么也没发生。另一个线程说我应该包含 Ws2tcpip.h 而不是 WinSock2 并添加 Ws2_32.lib。我已经添加了库,当我使用 include 时什么也没发生。到底是怎么回事?!
回答by Florian Haupt
The ip string can be converted to the in_addrstructure with the InetPtonfunction. It is used like this:
可以使用InetPton函数将 ip 字符串转换为in_addr结构。它是这样使用的:
InetPton(AF_INET, strIP, &ipv4addr)
You need to include the "Ws2tcpip.h" header file, use the library "Ws2_32.lib" and DLL "Ws2_32.dll".
您需要包含“Ws2tcpip.h”头文件,使用库“Ws2_32.lib”和DLL“Ws2_32.dll”。
回答by Nagev
Just to make the conversion clear. Let's say you have code using the deprecated inet_addras in this example:
只是为了使转换清晰。假设您有使用已弃用的inet_addr 的代码,如本例所示:
RecvAddr.sin_addr.s_addr = inet_addr("192.168.1.1");
It could be converted to the newer InetPtonas follows:
它可以转换为较新的InetPton,如下所示:
InetPton(AF_INET, _T("192.168.1.1"), &RecvAddr.sin_addr.s_addr);
The _Tmacro prevents the "const char incompatible with PCWSTR" error.
该_T宏防止“为const char不符合PCWSTR”的错误。
回答by Razin Bashar
You can try
你可以试试
#pragma warning(disable:4996)
for using inet_addr()
.
用于使用inet_addr()
.
回答by J CH
make sure you define _WINSOCK_DEPRECATED_NO_WARNINGS before all the include.
确保在所有包含之前定义 _WINSOCK_DEPRECATED_NO_WARNINGS。