首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

为什么获取地址总是失败?该怎么解决

2012-09-13 
为什么获取地址总是失败?#include stdio.h#include winsock2.h#pragma comment(lib,ws2_32.lib)int

为什么获取地址总是失败?
#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

热点排行