记得启动Winsock服务,例(获得主机IP和地址,支持MFC的Win32 Console Application):
#include "stdafx.h"
#include "localIP.h"
#include "winsock2.h"
#pragma comment (lib,"Ws2_32.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
hostent* host = gethostbyname("");
char* hostname=host->h_name;
char* IP=inet_ntoa(*(struct in_addr*)*(host->h_addr_list));
printf("Host Name:%s/n",hostname);
printf("Local IP:%s/n",IP);
WSACleanup();
return 0;
}