使用ACE获取主机的IP地址,不知道为什么会有127.0.0.1?
#include "stdafx.h" #include "aceOS.h" #include "aceSOCK_Connector.h" //用ACE获取本机IP address int _tmain(int argc, _TCHAR* argv[]) { WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2),&wsaData) != 0) return 0; //得到主机名 char hostname[MAXHOSTNAMELEN]; ACE_OS::hostname(hostname, sizeof (hostname)); printf("host name is %s ", hostname); printf("-------------------------- "); ACE_INET_Addr* addr_array1; size_t count = 0; if (ACE::get_ip_interfaces(count, addr_array1) != 0) { return 0; } //可以装下IPv6地址(46),IPv4为INET_ADDRSTRLEN(16) char address[INET6_ADDRSTRLEN]; //为了最后要删除空间,所以定义新的变量 ACE_INET_Addr* addr_array2 = addr_array1; for(size_t i=0; i<count; i++) { unsigned int net_order_ip = htonl(addr_array2->get_ip_address()); struct in_addr *paddr_stru = (in_addr*)&net_order_ip; printf("format 0: %s ", ACE_OS::inet_ntoa(*paddr_stru)); addr_array2->addr_to_string(address, sizeof (address), 1); printf("format 1: %s ", address); addr_array2++; printf("-------------------------- "); } //记得要delete[] addr_array; delete[] addr_array1; getchar(); return 0; }