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

I am using Visual Studio 2015 and trying to compile code that worked before I upgraded from VS 2013.

'inet_addr': use inet_pton () or InetPton () or define _WINSOCK_DEPRECATED_NO_WARNINGS

partner.sin_addr.s_addr = inet_addr(ip.c_str()); 

I tried to use the mentioned functions, but they were undefined. I tried to define a macro in many different places, but nothing happened. Another thread said that I should include Ws2tcpip.h instead of WinSock2 and add Ws2_32.lib. I already have a library, and when I used include, nothing happened. What's happening?!

+8
c ++ winsock
source share
4 answers

The ip string can be converted to an in_addr structure using InetPton . It is used as follows:

 InetPton(AF_INET, strIP, &ipv4addr) 

You need to include the header file "Ws2tcpip.h", use the library "Ws2_32.lib" and the DLL "Ws2_32.dll".

+8
source share

You can try

#pragma warning (disable: 4996)

for using inet_addr ().

+3
source share

Just to make the conversion understandable. 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 can be converted to the new 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.

+3
source share

make sure you specify _WINSOCK_DEPRECATED_NO_WARNINGS before all included.

0
source share

All Articles