vc gethostbyname用法

来源:互联网 发布:游族网络2017最新资讯 编辑:程序博客网 时间:2024/05/05 09:55

#include   <windows.h>  
        #include   <winsock.h>  
        #include   <stdio.h>  
   
        void   main()  
        {  
              WSADATA     wsaData;  
              char           szHostname[100];  
              HOSTENT   *pHostEnt;  
              int             nAdapter   =   0;  
              struct       sockaddr_in   sAddr;  
   
              if   (WSAStartup(0x0101,   &wsaData))  
              {  
                    printf("WSAStartup   failed   %s/n",   WSAGetLastError());  
                    return;  
              }  
   
              pHostEnt   =   gethostbyname(   "www.sina.com.cn");  
   
              while   (   pHostEnt->h_addr_list[nAdapter]   )  
              {  
                //   pHostEnt->h_addr_list[nAdapter]   is   the   current   address   in   host  
                //   order.  
   
                //   Copy   the   address   information   from   the   pHostEnt   to   a   sockaddr_in  
                //   structure.  
                    memcpy   (   &sAddr.sin_addr.s_addr,   pHostEnt->h_addr_list[nAdapter],  
                                      pHostEnt->h_length);  
   
                    //   Output   the   machines   IP   Address.  
                    printf("Name:         %s/nAddress:   %s/n",   pHostEnt->h_name,  
                                  inet_ntoa(sAddr.sin_addr));  
   
                    nAdapter++;  
              }  
              WSACleanup();  
              return;  
   
        }  

原创粉丝点击