#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#include <iostream>

#pragma comment(lib,"ws2_32.lib")


char buf[256]="";

struct hostent *ph = 0;

WSADATA w;

WSAStartup(0x0101, &w);//这一行必须在使用任何SOCKET函数前写!

gethostname(buf, 256);

std::string hostNmae = buf;//此处获得本机名称

ph = gethostbyname(buf);

const char *IP =inet_ntoa(*((struct in_addr *)ph->h_addr_list[0]));//此处获得本机IP

WSACleanup();

printf("%s",IP);