今天看了一下用gethostname和gethostbyname 这两个函数 获取本机地址 记录如下
WSADATA data;
WORD ss = MAKEWORD(2,0);
::WSAStartup( ss,&data );//初始化套接字
in_addr addr;
char buff[20];
gethostname(buff,sizeof(buff));
struct hostent* hostaddr;
hostaddr = gethostbyname( buff );
memcpy( &addr.s_addr,hostaddr->h_addr_list[0],sizeof(addr.s_addr) );
strcpy(buff,inet_ntoa(addr));
buff[20]=0;
std::string str = buff;
std::cout << str << std::endl;