得到IP
2012-09-19 17:14 龙成 阅读(144) 评论(0) 编辑 收藏 举报// GetIp.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <winsock2.h> #include <iostream> #pragma comment(lib,"WS2_32.lib") using namespace std; int _tmain(int argc, _TCHAR* argv[]) { WSADATA wsd; if(WSAStartup(MAKEWORD(2,2),&wsd) != 0) { return -1; } char szHostname[100],szHostaddress[200]; if(gethostname(szHostname,sizeof(szHostname)) != SOCKET_ERROR) { HOSTENT *pHostEnt = gethostbyname(szHostname); if(pHostEnt != NULL) { sprintf(szHostaddress,"%d.%d.%d.%d", (pHostEnt->h_addr_list[0][0]&0x00ff), (pHostEnt->h_addr_list[0][1]&0x00ff), (pHostEnt->h_addr_list[0][2]&0x00ff), (pHostEnt->h_addr_list[0][3]&0x00ff)); } } cout << szHostaddress << endl; return 0; }