Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

winsock

I'm using Visual Studio 2015 and attempting to compile code that has worked before I updated from VS 2013.

'inet_addr': Use inet_pton() or InetPton() instead or define _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. I already have the library added, and when I used the include nothing happened. What is going on?!

like image 543
The Count Avatar asked Apr 18 '16 00:04

The Count


People also ask

Why is Inet_addr deprecated?

Blankly recommending to set this define reinforces bad style and delays what the warning points to: the need for modernizing the code. The use of inet_addr for example causes thread-safety issues. Even locally disabling the deprecation warning would be a better option since it makes the issue visible.

What to use instead of inet_ addr?

Another way you may have a try is to add #include <WS2tcpip. h> and use inet_pton(AF_INET, IP, buf) to instead of inet_addr(IP). In addition, you can check if your properties is settings like this: Project properties -> Configuration Properties -> C/C++ -> General -> SDL checks -> No.


3 Answers

Just to make the conversion clear. Let's say you have code using the deprecated inet_addr as in this example:

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

It could be converted to the newer InetPton as follows:

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

The _T macro prevents the "const char incompatible with PCWSTR" error.

like image 179
Nagev Avatar answered Oct 16 '22 14:10

Nagev


The ip string can be converted to the in_addr structure with the InetPton function. 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".

like image 23
Florian Haupt Avatar answered Oct 16 '22 14:10

Florian Haupt


You can try

#pragma warning(disable:4996) 

for using inet_addr().

like image 11
Razin Bashar Avatar answered Oct 16 '22 13:10

Razin Bashar