gethostname和gethostbyname,inet_ntoa

// socketTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <WinSock2.h> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { WSADATA wsData = { 0 }; int nRet = ::WSAStartup(MAKEWORD(2, 2), &wsData); char szBuf[MAX_PATH] = { 0 }; ::gethostname(szBuf, MAX_PATH);//获取计算机名 struct hostent* pStHostent = NULL; strcpy(szBuf, "www.google.com"); pStHostent = ::gethostbyname(szBuf);//传入域名或者主机名,得到指向host结构 /* pStHostent->h_name; //规范名 pStHostent->h_addrtype;//地址类型IPV4 or IPV6 pStHostent->h_addr_list;//ip地址 pStHostent->h_length; //ip地址长度 pStHostent->h_aliases;//主机别名 */ cout << pStHostent->h_name << endl; char** pptr = NULL; struct in_addr addr; switch(pStHostent->h_addrtype) { case AF_INET: case AF_INET6: pptr = pStHostent->h_addr_list; for (; *pptr != NULL; pptr++) { //addr.S_un.S_addr = *(u_long*)*pptr; addr.s_addr = *(u_long*)*pptr; //打印出IP地址列表 cout << inet_ntoa(addr) << endl; } break; default: cout << "UnKnow addr type!" << endl; } //打印出域名或者计算机名对应的别名列表 pptr = pStHostent->h_aliases; for (; *pptr != NULL; pptr++) { cout << *pptr << endl; } ::WSACleanup(); return 0; }

你可能感兴趣的:(struct,null,Path)