tqsheng

go.....
随笔 - 366, 文章 - 18, 评论 - 101, 引用 - 0
数据加载中……

名字与IP地址转换编程


一、实验目的

理解名字与IP地址的转换函数,实现主机名与IP地址之间的转换。学习和掌握Linux下的gethostbyname()和gethostbyaddr()函数基本原理和基本编程方法。

二、实验平台

ubuntu-8.04操作系统

三、实验内容

1、利用gethostbyname()函数编程实现名字解析,将主机名转换成相应IP地址。

2、利用gethostbyaddr()函数编程实现反向地址解析,将IP地址转换成主机名,查询指定IP地址对应的主机域名地址。

四、实验原理

现在的网络都是使用名字来访问服务器的,而不是使用IP地址来访问。那它们是怎么转换的呢?答案就是利用名字与IP地址的转换函数实现的:gethostbyname和gethostbyaddr在主机名字与IP地址间进行转换。

1、gethostbyname()函数

找主机名最基本的函数gethostbyname(),该函数执行如果成功,它返回一个指向结构hostent的指针,该结构中包含了该主机的所有IPv4地址或IPv6地址;如果失败返回空指针。下面是定义:

-------------------------------------------------------------------
# include <netdb.h>

struct hostent * gethostbyname (const char *hostname);

 -------------------------------------------------------------------

参数hostname是主机的域名地址,函数将查询的结果作为参数返回。如果失败返回空指针;如果成功此参数返回的非空指针指向如下的hostent结构:

 

-------------------------------------------------------------------
struct hostent      {

char  * h_name;              /*主机的正式名称*/

char  * * h_aliases;         /*主机的别名列表*/

int         h_addrtype;  /*主机地址类型*/

int         h_length;   /*主机地址长度*/

char  * * h_addr_list;            *主机IP地址的列表*/

};

# define h_addr h_addr_list[0]             /*在列表中的第一个地址*/

 -------------------------------------------------------------------

 

 

2、gethostbyaddr()函数

gethostbyaddr()函数的作用是可以查询指定的IP地址对应的主机域名地址。函数的形式如下:

-------------------------------------------------------------------
# include <netdb.h>

struct hostent * gethostbyaddr (const char *addr, size_t len, int family);

 -------------------------------------------------------------------

返回:若为非空指针,则表示成功;若为空指针,则表示出错,同时设置h_errno。该函数同样返回一个指向结构hostent的指针。而在参数中,参数addr不是char *类型,而是一个真正指向含有IPv4或IPv6地址的结构in_addr或in6_addr的指针;len是此结构的大小,对于IPv4地址为4,对于IPv6地址为16;参数family为AF_INET或AF_INET6。

 

五、实验步骤

1、登陆进入ubuntu操作系统,新建一个文件,命名为gethostbyname.c,新建另一个文件,命名为gethostbyaddr.c。

2、在gethostbyname.c和gethostbyaddr.c中编写相应代码并保存。

3、在“终端”(“Applications”→“附件”→“终端”)中执行命令进入gethostbyname.c和gethostbyaddr.c所在目录。(pwd命令可以显示当前所在目录;ls命令可以显示当前目录下的文件和文件夹信息;cd..命令可以进入上一级目录;cd 目录名 命令可以进入当前所示的某个目录。)

4、执行命令gcc –o gethostbyname gethostbyname.c生成可执行文件gethostbyname。

5、执行命令./gethostbyname host_name。(注意:此处host_name代表主机名,利用命令hostname可以查看本机的主机名)

6、观察并分析程序运行结果。

7、执行命令gcc –o gethostbyaddr gethostbyaddr.c生成可执行文件gethostbyaddr。

8、执行命令./gethostbyname 127.0.0.1,观察结果。

9、认真分析源代码,体会gethostbyname()和gethostbyaddr()函数的应用。

六、参考程序

1、gethostbyname.c内容如下:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <sys/types.h>  
  5. #include <sys/socket.h>  
  6. #include <netinet/in.h>  
  7. #include <arpa/inet.h>  
  8. #include <netdb.h>  
  9.    
  10. main(int argc, const char **argv)  
  11. {  
  12.     ulong addr;  
  13.     struct hostent*hp;  
  14.     char **p;  
  15.     if (argc !=2) {  
  16.         (void)printf("usage: %s host_name\n", argv[0]);  
  17.         exit(1);  
  18.         }  
  19.     hp =gethostbyname(argv[1]);  
  20.     if (hp ==NULL) {  
  21.         (void)printf("host information for %s not found\n", argv[1]);  
  22.         exit(2);  
  23.         }  
  24.     for (p =hp->h_addr_list; *p != 0; p++) {  
  25.         structin_addr in;  
  26.         char**q;  
  27.         (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));  
  28.         (void)printf("%s\t%s", inet_ntoa(in), hp->h_name);  
  29.         for(q = hp->h_aliases; *q != 0; q++)  
  30.             (void) printf(" %s", *q);  
  31.         (void)putchar('\n');  
  32.         }  
  33.     exit (0);  
  34. }  
  35.    


 

2、gethostbyaddr.c内容如下:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <sys/types.h>  
  5. #include <sys/socket.h>  
  6. #include <netinet/in.h>  
  7. #include <arpa/inet.h>  
  8. #include <netdb.h>  
  9.    
  10. main(int argc, const char **argv)  
  11. {  
  12.      ulong addr;  
  13.      structhostent *hp;  
  14.      char **p;  
  15.      if (argc !=2) {  
  16.         (void)printf("usage: %s IP-address\n", argv[0]);  
  17.         exit(1);  
  18.         }  
  19.      if((int)(addr = inet_addr(argv[1])) == -1) {  
  20.         (void)printf("IP-address must be of the form a.b.c.d\n");  
  21.         exit(2);  
  22.         }  
  23.      hp =gethostbyaddr((char *)&addr, sizeof (addr), AF_INET);  
  24.      if (hp ==NULL) {  
  25.         (void)printf("host information for %s not found\n", argv[1]);  
  26.         exit(3);  
  27.         }  
  28.      for (p =hp->h_addr_list; *p != 0; p++) {  
  29.         structin_addr in;  
  30.         char**q;  
  31.         (void)memcpy(&in.s_addr, *p, sizeof (in.s_addr));  
  32.         (void)printf("%s\t%s", inet_ntoa(in), hp->h_name);  
  33.         for(q = hp->h_aliases; *q != 0; q++)  
  34.                   (void) printf(" %s", *q);  
  35.         (void)putchar('\n');  
  36.         }  
  37.      exit (0);  
  38. }  
  39.    

 

posted on 2012-07-05 14:25 tqsheng 阅读(301) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理