error C4996: ‘inet_addr‘: Use inet_pton() or InetPton() instead or define _WINSOCK_D

[questions]

Vs2015 appears when writing socket client. (this is the second time I have encountered this problem. Last time vs2017, I must remember it to facilitate myself to check in the future: P)

SOCKADDR_ IN ClientAddr;
ClientAddr.sin_ family = AF_ INET;
ClientAddr.sin_ addr.S_ un.S_ addr = inet_ addr(“127.0.0.1”);
ClientAddr.sin_ Port = htons (port);
error c4996: ‘INET_ addr’: Use inet_ pton() or InetPton() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARNINGS to disable deprecated API warnings

[reason]

In the version after vs2013, INET is added_ New functions such as Pton () and inetpton () are used for IP address conversion between “dotted decimal” and “binary integer”, and can handle IPv4 and IPv6. And INET_ Addr is an old function. Higher version vs uses a new function by default when compiling, so this error will be reported.

[solution 1]

Replace INET with the new function suggested_ Addr function.

[solution 2]

Modify the vs configuration and tell it I want the old function

1. Modification method: Item – & gt; attribute – & gt; C / C + + – & gt; general – & gt; SDL check, change “yes” to “no”.
2. Property page of file_ CRT_ SECURE_ NO_ “Warnings” plus
– – –
copyright notice: This is CSDN blogger Rani_ The original article of “ZZ” follows CC 4.0 by-sa copyright agreement. Please attach the link of original source and this statement.
Link to the original text: https://blog.csdn.net/weixin_ 42731241/article/details/96143934

Read More: