为什么获取地址总是失败?
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main()
{
char srcIP[]="";
char hostname[128]="";
struct hostent *phe;
gethostname(hostname,128);//获取本机主机名
phe =gethostbyname(hostname);//获取本机ip地址结构
if(phe == NULL)
{
printf("Get LocalIP Error...\n");
return 0;
}
strcpy(srcIP, inet_ntoa(*((struct in_addr *)phe->h_addr_list[0])));//得到本机ip地址
printf("%s",srcIP);
return 0;
}
[解决办法]
具体什么错误
[解决办法]
大哥,你还得先使用: WSAStartup初始化套接字库
记住使用socket编程,使用socket函数,都先WSAStartup