简体   繁体   English

'inet_addr':使用 inet_pton() 或 InetPton() 代替或定义 _WINSOCK_DEPRECATED_NO_WARNINGS

[英]'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS

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

in this code:在这段代码中:

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.另一个线程说我应该包含 Ws2tcpip.h 而不是 WinSock2 并添加 Ws2_32.lib。 I already have the library added, and when I used the include nothing happened.我已经添加了库,当我使用 include 时什么也没发生。 What is going on?!这是怎么回事?!

Just to make the conversion clear.只是为了使转换清晰。 Let's say you have code using the deprecated inet_addr as in this example:假设您有使用已弃用的inet_addr 的代码,如本例所示:

RecvAddr.sin_addr.s_addr = inet_addr("192.168.1.1");

It could be converted to the newer InetPton as follows:它可以转换为较新的InetPton ,如下所示:

InetPton(AF_INET, _T("192.168.1.1"), &RecvAddr.sin_addr.s_addr);

The _T macro prevents the " const char incompatible with PCWSTR " error. _T宏可防止“ const char 与 PCWSTR 不兼容”错误。

The ip string can be converted to the in_addr structure with the InetPton function.可以使用InetPton函数将 ip 字符串转换为in_addr结构。 It is used like this:它是这样使用的:

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”。

You can try你可以试试

#pragma warning(disable:4996) 

for using inet_addr() .用于使用inet_addr()

确保在所有包含之前定义 _WINSOCK_DEPRECATED_NO_WARNINGS。

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

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