为什么机器有2个ip?一张网卡,联通用户
PC2011192.168.0.1
110.53.1.72
程序代码:
#define WIN32_LEAN_AND_MEAN #include <iostream.h> #include <windows.h> #include <winsock2.h> #pragma comment(lib,"ws2_32") class Cinitsock { public: Cinitsock(BYTE x=2,BYTE y=2) { WSADATA ws; WSAStartup( MAKEWORD(x,y),&ws ); } ~Cinitsock() { WSACleanup(); } }; int main() { Cinitsock cinit; char szhost[256] ={0}; gethostname(szhost,256);//获取主机名 cout<<szhost<<endl; hostent* phost=gethostbyname(szhost); in_addr addr; for (int j=0;;j++)//获取本机所有ip { char* p= phost->h_addr_list[j]; if (!p) { break; } memcpy(&addr.S_un.S_addr,p,phost->h_length); char* ptr =inet_ntoa(addr); cout<<ptr<<endl; } return 0; }