通过gethostbyaddr来获取计算机的名称

通过gethostbyaddr来获取计算机的名称。如下代码:

 

#include  < stdio.h >
#include
< winsock2.h >
#include 
< Ws2tcpip.h >
#pragma  comment(lib,"ws2_32.lib")
int  main( int  argc,  char *  argv[])
{
    WORD wVersionRequested;
    WSADATA wsaData;
    
int err;
    wVersionRequested 
= MAKEWORD( 22 );
    err 
= WSAStartup( wVersionRequested, &wsaData );
    
if ( err != 0 ) {
        
/* Tell the user that we could not find a usable */
        
/* WinSock DLL.                                  */
        
return 1;
    }


    
/* Confirm that the WinSock DLL supports 2.2.*/
    
/* Note that if the DLL supports versions later    */
    
/* than 2.2 in addition to 2.2, it will still return */
    
/* 2.2 in wVersion since that is the version we      */
    
/* requested.                                        */

    
if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) 
!= 2 ) {
            
/* Tell the user that we could not find a usable */
            
/* WinSock DLL.                                  */
            WSACleanup( );
            
return 1
        }

    
    
struct hostent *host; 
    
char **alias = NULL;   
    
if(argc<2
    

         fprintf(stderr,
"Usage:%s hostname|ip.. a",argv[0]); 
         
return 0;
    }
 
    argv
++
    
for(;*argv!=NULL;argv++
    

         unsigned 
long ddd = inet_addr(*argv);
         
if(ddd != INADDR_NONE)
         

           host
=gethostbyaddr((char   *)&ddd,4,AF_INET); 
         }
  
         
else  
         

              host
=gethostbyname(*argv);
         }
 
        
if(host==NULL) 
        

              fprintf(stderr,
"No address information of %s ",*argv);           
               
return 1;
        }
 
        printf(
"Official host name %s ",host->h_name); 
        printf(
"Name aliases:"); 
        
for(alias=host->h_aliases;*alias!=NULL;alias++
          printf(
"%s ,",*alias); 
        printf(
" Ip address:"); 
        
for(alias=host->h_addr_list;*alias!=NULL;alias++
          printf(
"%s ,",inet_ntoa(*(struct in_addr *)(*alias))); 
    }
 
    WSACleanup();
    
return 1;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值