获取计算机IP地址

#include<winsock2.h>
#include<stdio.h>
#pragma comment(lib,"WS2_32.lib")
char * getstrIp()
{
 char * pstrIp = new char[ 20 ];
 ZeroMemory( pstrIp, 20 );

 WSADATA data;
 WSAStartup(2,&data);
 hostent* host = gethostbyname("");
 char *IP = inet_ntoa(*(struct in_addr*)*(host->h_addr_list));
 printf("Local IP:%s\n",IP);  
 memcpy( pstrIp, IP, strlen( IP ) );
 WSACleanup();

 return pstrIp;
 
}

你可能感兴趣的:(获取计算机IP地址)