发新话题
打印

[转载]获取本机IP地址代码

[转载]获取本机IP地址代码

原始连接:http://www.blacksky.cn/article.asp?id=408
复制内容到剪贴板
代码:
////////////////////以下代码在VC++ 6.0 + SP6直接编译成功



#include <stdio.h>

#include <winsock2.h>



#pragma comment( lib, "WS2_32.lib" ) //很多人就是因为这个编译不成功 :)



int main()

{

char host_name[256];

int WSA_return;

WSADATA WSAData;

HOSTENT *host_entry;

WORD wVersionRequested;



wVersionRequested = MAKEWORD( 2, 0 );

WSA_return=WSAStartup(wVersionRequested,&WSAData); //进行WSAStartup函数调用



if(WSA_return==0)

{

gethostname(host_name,sizeof(host_name)); //获取本地主机名称

host_entry=gethostbyname(host_name); //从主机名数据库中得到对应的“主机”



  for( int i = 0; host_entry!= NULL && host_entry->h_addr_list[i]!= NULL; i++ )

    //循环得出本地机器所有IP地址

  {

    const char *pszAddr=inet_ntoa (*(struct in_addr *)host_entry->h_addr_list[i]);

MessageBox(NULL,pszAddr,host_name,MB_OK);

  }



}

else

{

  //错误提示

printf("ERROR !\n");

}



WSACleanup();

return 0;

}
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

不行啊
c:\documents and settings\xpsniper\桌面\a.c(47) : error C2143: syntax error : missing &#39;;&#39; before &#39;type&#39;
c:\documents and settings\xpsniper\桌面\a.c(47) : error C2143: syntax error : missing &#39;;&#39; before &#39;type&#39;
c:\documents and settings\xpsniper\桌面\a.c(47) : error C2143: syntax error : missing &#39;)&#39; before &#39;type&#39;
c:\documents and settings\xpsniper\桌面\a.c(47) : error C2143: syntax error : missing &#39;;&#39; before &#39;type&#39;
c:\documents and settings\xpsniper\桌面\a.c(47) : error C2065: &#39;i&#39; : undeclared identifier
c:\documents and settings\xpsniper\桌面\a.c(47) : error C2059: syntax error : &#39;)&#39;
c:\documents and settings\xpsniper\桌面\a.c(51) : error C2143: syntax error : missing &#39;;&#39; before &#39;{&#39;

TOP

发新话题