VS2015下调试获取本地IP,出现错误warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API
百度解决问题:需要在项目属性里设置,告诉编译器,我就用老函数,让她不要报错了。
//Project properties -> Configuration Properties -> C/C++ -> General -> SDL checks -> No
原文地址:http://jingyan.baidu.com/article/1709ad8097e5904634c4f03e.html
获取本地IP代码实现:
#include <winsock2.h> //套接字编程需要的头文件
#pragma comment(lib,"ws2_32.lib") //套接字编程需要的库文件
//Project properties -> Configuration Properties -> C/C++ -> General -> SDL checks -> No
CString CboardtestDlg::GetLocalIp(void)
{
struct hostent* phost;
char ip[20];
char hostname[100];
WSADATA wsadata;
if (0 != WSAStartup(MAKEWORD(2, 2), &wsadata)) //初始化
{
AfxMessageBox("初始化网络环境失败!");
return NULL;
}
gethostname(hostname, 100);
phost = gethostbyname(hostname);
char *pIP = inet_ntoa(*(in_addr *)phost->h_addr_list[0]);//将32位IP转化为字符串IP
WSACleanup(); //释放Winsock API
CString LocalIp = pIP;
return LocalIp;
}
已测试