现在的位置: 首页 > 综合 > 正文

获取本机IP地址列表

2012年02月13日 ⁄ 综合 ⁄ 共 703字 ⁄ 字号 评论关闭
#include <list>
#include 
<string>
using namespace std;

#include 
<Winsock2.h>
#pragma comment(lib,"Ws2_32.lib")

int GetIPList(list<string> &iplist)
{
WSADATA wsa;
if( WSAStartup( 0x22&wsa ) != 0 )
{
   
return -1;
}


iplist.clear();

char name[128];
if-1 == gethostname(name, sizeof(name)) )
{
   WSACleanup();
   
return -1;
}


struct hostent FAR * pHostent;
pHostent 
= gethostbyname(name);
if( NULL == pHostent )
{
   WSACleanup();
   
return -1;
}

char **= pHostent->h_addr_list;
for( ; *!= NULL; p++ )
{
   
string strip = inet_ntoa( *(IN_ADDR*)(*p) );
   iplist.push_back( strip );
}

WSACleanup();
return (int)iplist.size();
}


int _tmain(int argc, _TCHAR* argv[])
{
list
<string> iplist;
GetIPList( iplist );

return 0;
}

 

抱歉!评论已关闭.