测试平台:VC6.0
附加:Project-Settings-Link在Object/ Library modules:中添加ws2_32.lib
代码:
#
include <iostream>
# include <winsock.h>
using namespace std;
void main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD( 2 , 2 ), & wsaData))
return ;
hostent * phost;
char ip[ 20 ];
char hostname[ 50 ];
if (gethostname(hostname, 50 ))
return ;
phost = gethostbyname(hostname);
char * * iplist;
iplist = phost -> h_addr_list;
while ( * iplist)
{
strcpy(ip, inet_ntoa( * (in_addr * ) * iplist));
iplist ++ ;
}
cout << hostname << endl;
cout << ip << endl;
WSACleanup();
}
# include <winsock.h>
using namespace std;
void main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD( 2 , 2 ), & wsaData))
return ;
hostent * phost;
char ip[ 20 ];
char hostname[ 50 ];
if (gethostname(hostname, 50 ))
return ;
phost = gethostbyname(hostname);
char * * iplist;
iplist = phost -> h_addr_list;
while ( * iplist)
{
strcpy(ip, inet_ntoa( * (in_addr * ) * iplist));
iplist ++ ;
}
cout << hostname << endl;
cout << ip << endl;
WSACleanup();
}