#include"stdio.h"
#include"string.h"
#include"Winsock2.h"
#pragma comment(lib,"WS2_32.lib")
printf("********************************\n");
printf("IP OUTPUT\n");
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR)
{
exit(0);
}
int nLen = 256;
char hostname[20];
gethostname(hostname, nLen);
hostent *pHost = gethostbyname(hostname);
LPSTR lpAddr = pHost->h_addr_list[0];
struct in_addr inAddr;
memmove(&inAddr, lpAddr, 4);
printf("有線IP地址:%s\n", inet_ntoa(inAddr));
memmove(&inAddr, lpAddr + 4, 4);
printf("無線IP地址:%s\n", inet_ntoa(inAddr));
memmove(&inAddr, lpAddr + 8, 4);
printf("環回IP地址:%s\n", inet_ntoa(inAddr));
memmove(&inAddr, lpAddr + 12, 4);
printf("虛擬機1 IP地址:%s\n", inet_ntoa(inAddr));
memmove(&inAddr, lpAddr + 16, 4);
printf("虛擬機2 IP地址:%s\n", inet_ntoa(inAddr));
printf("********************************\n");
可先參考 cmd 輸入ipconfig 確認ip
測試環境 :MFC 2015