#include<stdio.h>
CInitSock initSock;
void main()
{
char szHost[256];
//get host name
::gethostname(szHost,256);
// get add info from host
hostent *pHost=::gethostbyname(szHost);
//print all the add of ips
in_addr addr;
for(int i=0;;i++)
{
char *p=pHost->h_addr_list[i];
if(p==NULL)
break;
memcpy(&addr.S_un.S_addr,p,pHost->h_length );
char *szIp=::inet_ntoa(addr);
printf("本机IP地址:%s \n",szIp);
}
}
#pragma comment(lib,"WS2_32")
class CInitSock
{
public:
CInitSock(BYTE minorVer=2,BYTE majorVer=2)
{
//init WS2_32.dll
WSADATA wsaData;
WORD sockVersion=MAKEWORD(minorVer,majorVer);
if(::WSAStartup(sockVersion,&wsaData)!=0)
{
exit(0);
}
~CInitSock()
{
::WSACleanup ();
}
};