#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //winsock 2.2 library
// 获取本机ip,winsock
// 来源于网络
typedef struct {
char FAR * h_name;
char FAR * FAR * h_aliases;
short h_addrtype;
short h_length;
char FAR * FAR * h_addr_list;
} hostent ;
int main()
{
char host[100];
hostent *local;
//Initialise Winsock
WSADATA wsock;
if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)
{
fprintf(stderr,"WSAStartup() failed");
exit(EXIT_FAILURE);
}
gethostname(host,sizeof(host));
local = (hostent*)gethostbyname(host);
printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",
local->h_name, local->h_aliases, local->h_addrtype, local->h_length);
printf("IP Address : %s\n",inet_ntoa(*((struct in_addr *)local->h_addr)));
if (WSACleanup() == SOCKET_ERROR)
{
printf("WSACleanup failed with error %d\n", WSAGetLastError());
}
return 0;
}