zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++获取站点的ip地址

C++地址IP 获取 站点
2023-09-11 14:21:03 时间


  1. #include "stdafx.h" 
  2. #include <winsock2.h> 
  3. #pragma comment (lib,"ws2_32.lib") 
  4. int _tmain(int argc, _TCHAR* argv[]) 
  5. {  
  6.  
  7. p: printf("输入地址:"); 
  8. char szHost[256]; 
  9. memset(szHost,0,256); 
  10. scanf("%s",szHost); 
  11. unsigned long lAddr = inet_addr(szHost); 
  12. if (lAddr == INADDR_NONE) 
  13.   hostent *pHE = gethostbyname(szHost); 
  14.   if (!pHE) 
  15.    goto p; 
  16.   sockaddr_in sa; 
  17.   for (int iHost=0;pHE->h_addr_list[iHost];iHost++)  
  18.   { 
  19.    char *ip; 
  20.    unsigned int port; 
  21.    memcpy(&sa.sin_addr.s_addr, pHE->h_addr_list[iHost], pHE->h_length); 
  22.    ip = inet_ntoa(sa.sin_addr);printf("%s\n",ip); 
  23.   } 
  24. goto p;