当前位置:   article > 正文

Linux下gethostbyname_h_addr_list_linux gethostbyname h_addr

linux gethostbyname h_addr





  struct hostent
{
  char* h_name;
  char** h_aliases;
  short h_addrtype;
  short h_length;
  char**h_addr_list;
};


//linux
#include<netdb.h>
struct hostent *  gethostbyname(const char*hostname);
error:return NULL and set  h_errno


  1. //compile:gcc gethostbyname.c
  2. //run:./a.out
  3. #include<stdio.h>
  4. #include<netdb.h>
  5. #include<unistd.h>
  6. #include<string.h>
  7. int main(int argc,char* argv[])
  8. {
  9. char ret_hostname[65];
  10. gethostname(ret_hostname,sizeof(ret_hostname));;//#include<unistd.h>
  11. printf("by gethostname() get hostname:%s\n",ret_hostname);
  12. struct hostent *ret_hostent;
  13. ret_hostent=gethostbyname(ret_hostname);
  14. printf("h_name %s\nh_aliases %s\nh_addrtype %d\nh_length %d\nh_addr_list %s\n",
  15. ret_hostent->h_name,
  16. *(ret_hostent->h_aliases),
  17. ret_hostent->h_addrtype,
  18. ret_hostent->h_length,
  19. *(ret_hostent->h_addr_list+1));
  20. printf("sizeof(h_addr_list[0]) is %lu\n",sizeof(ret_hostent->h_addr_list[0]));//long unsigned int
  21. printf("strlen(h_addr_list[0]) is %zu\n",strlen(ret_hostent->h_addr_list[0]));//size_t
  22. //type: int char char* unsigned int long unsigned int size_t float double long int
  23. //format:%d %c %s %u %lu %zu %f %lf %ld
  24. printf("%d %d %d %d\n",ret_hostent->h_addr_list[0][0]&0xFF,ret_hostent->h_addr_list[0][1]&0xFF,ret_hostent->h_addr_list[0][2]&0xFF,
  25. ret_hostent->h_addr_list[0][3]&0xFF);//big_endian
  26. return 0;}
  27. /*
  28. struct hostent
  29. {
  30. char* h_name;
  31. char** h_aliases;
  32. short h_addrtype;
  33. short h_length;
  34. char**h_addr_list;
  35. };
  36. //linux
  37. #include<netdb.h>
  38. struct hostent * gethostbyname(const char*hostname);
  39. error:return NULL and set h_errno
  40. */


out in screen 屏幕打印
ubuntu@linuxc_easy:~/linuxc_easy/pcdtest$ gcc gethostbyname.c                                                                                                             
ubuntu@linuxc_easy:~/linuxc_easy/pcdtest$ ./a.out                                                                                                                         
by gethostname() get hostname:linuxc_easy                                                                                                                                 
h_name linuxc_easy                                                                                                                                                        
h_aliases (null)                                                                                                                                                          
h_addrtype 2                                                                                                                                                              
h_length 4                                                                                                                                                                
h_addr_list (null)                                                                                                                                                        
sizeof(h_addr_list[0]) is 8                                                                                                                                               
strlen(h_addr_list[0]) is 2                                                                                                                                               
172 17 0 4    


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/483145
推荐阅读
相关标签
  

闽ICP备14008679号