C++ 获取本机全部IP地址(有不止一个IP的情况)

typedef struct tagIPInfo { char ip[30]; }IPInfo; bool GetLocalIPs(IPInfo* ips,int maxCnt,int* cnt) { //1.初始化wsa WSADATA wsaData; int ret=WSAStartup(MAKEWORD(2,2),&wsaData); if (ret!=0) { return false; } //2.获取主机名 char hostname[256]; ret=gethostname(hostname,sizeof(hostname)); if (ret==SOCKET_ERROR) { return false; } //3.获取主机ip HOSTENT* host=gethostbyname(hostname); if (host==NULL) { return false; } //4.逐个转化为char*并拷贝返回 *cnt=host->h_length<maxCnt?host->h_length:maxCnt; for (int i=0;i<*cnt;i++) { in_addr* addr=(in_addr*)*host->h_addr_list; strcpy(ips[i].ip,inet_ntoa(addr[i])); } return true; } 

单个IP的情况:http://blog.csdn.net/iamoyjj/archive/2011/05/29/6452698.aspx

你可能感兴趣的:(C++,struct,socket,null)