Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563615
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-03-29 16:03:51

网络编程中,地址转换函数比较常用的有将主机字节存储顺序与网络字节存储顺序进行互换的函数。
1. htons<--> ntohs ;2. htonl <--> ntohl
通常情况下,网络字节序列(或是通信协议协议)中数据的顺序是大端模式,
就是将数据的高位存放到低地址,将数据的地位存放到高地址空间中;而操作系统中采用的便是小端模式:
高位地址存高位数据,低地址存低位数据。

htons : h(host) to n(net) s (short): 用来将主机short类型的字节顺序转为网络字节顺序。
ntohs : n(net) to h(host) s(short) : 用来将网络 short 类型的字节顺序转为主机字节顺序。

htonl : h(host) to n (net) l (long ) : 用来将主机字节顺序转换为网络字节顺序,只不过转换变量的类型不同为 unsigned long
ntohl:  n (net) to h(host) l (long) : 用来将网络字节顺序转换为主机字节顺序,转换的变量为 unsigned long

这些函数是大端小端编码模式的转换
下面要介绍的是 IP 地址字符串的点分格式转换为十六进制的函数

inet_aton 是用来将点分格式的字符串IP 地址转换为二进制格式的IP地址,
同时该二进制格式的IP 地址遵循的是网络字节顺序

inet_ntoa 是用来将网络字节顺序的二进制IP 地址转换为字符串表示的点分格式IP 地址。




inet_aton 函数 API 描述
#include
#include
int inet_aton ( const char *cp , struct in_addr *in ) ;
参数:
 1. cp 指向点分格式IP地址字符串指针,"xxx.xxx.xxx.xxx"
 2. in 它是 值-结果 类型的参数,用做提取与存放函数转换后的结果,其中 struct in_addr 类型与
            struct sockaddr_in 中的sin_addr  字段类型是一致的,struct in_addr 中的 s_addr 变量存放着
           16进制网络字节顺序的IP地址
返回值:执行成功返回非零数值,失败返回 0



inet_ntoa 函数 API 描述
#include
#include
char * inet_ntoa( struct in_addr_in ) ;

参数:
   in : 该参数存放用来转换的16进制网络顺序的IP地址结构体
返回值:
   如果函数 Inet_ntoa 成功将 16进制网络顺序的 IP地址转换为字符串类型的点分IP 地址,
   则将该字符串作为返回值返回,转换失败,返回 NULL



实例代码1:
下面的程序将手动输入的点分十进制IP地址转换为 16进制网络顺序的 IP 地址

点击(此处)折叠或打开

  1. //inet_atonTest.cpp

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. //#include <sys/socket.h>
  6. //#include <netinet/in.h>
  7. #include <arpa/inet.h> // inet_aton(2)


  8. int main ( int c , char ** v )
  9. {
  10.  char buffer[32] ;
  11.  int ret ;
  12.  struct in_addr in ;
  13.  // struct in_addr in sin_addr of struct sockaddr_in

  14.   memset ( &in , 0 , sizeof(struct in_addr )) ;
  15.   printf ("input ip address like xxx.xxx.xxx.xxx \n") ;
  16.   scanf ("%s" , buffer) ;
  17.   
  18.   if( inet_aton( buffer , &in ) )
  19.   {
  20.     printf ("before change buffer : %s \n", buffer) ;
  21.     printf ( "after inet_aton ip : 0x%x \n" , in.s_addr ) ;
  22.   }
  23.   else
  24.   {
  25.     perror ("inet_aton error") ;
  26.     goto error ;
  27.   }

  28.   error :
  29.     printf ("main error") ;
  30.     goto success ;
  31.  success:
  32.     return 0 ;
  33. }
运行结果:
input ip address like xxx.xxx.xxx.xxx 
127.0.0.1
transfer 127.0.0.1 into 0x100007f 
实例代码2
下面的程序是用来将用户手动输入的点分十进制 IP 地址转换为16进制的网络顺序地址,
将该网络顺序地址输出,然后再次将其转换为字符串点分十进制IP地址

点击(此处)折叠或打开

  1. //inet_ntoa.cpp

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. //#include <sys/socket.h>
  6. //#include <netinet/in.h>
  7. #include <arpa/inet.h> // inet_aton(2)

  8. int stringToAddr ( const char * buffer, struct in_addr *sin_addr )
  9. {
  10.   int ret ;
  11.   
  12.   if ( ( ret = inet_aton ( buffer , sin_addr )) != 0 )
  13.   {
  14.     printf ("transfer %s into 0x%x \n" , buffer , (*sin_addr).s_addr ) ;
  15.   }
  16.   else
  17.   {
  18.     perror ("inet_aton") ;
  19.   }
  20.  
  21.   return ret ;
  22. }

  23. int addrToString ( char *buffer , struct in_addr sin_addr )
  24. {
  25.   
  26.   if ( (buffer = inet_ntoa (sin_addr) ) != NULL )
  27.   {
  28.     printf ("transfer 0x%x into %s \n" , sin_addr.s_addr , buffer) ;
  29.     return 0 ;
  30.   }
  31.   
  32.   perror ("inet_ntoa") ;
  33.   
  34.   return -1 ;
  35. }

  36. int main ( int c , char ** v )
  37. {
  38.  char buffer[32] ;
  39.  int ret ;
  40.  struct in_addr in ;
  41.  // struct in_addr in sin_addr of struct sockaddr_in

  42.   memset ( &in , 0 , sizeof(struct in_addr )) ;
  43.   printf ("input ip address like xxx.xxx.xxx.xxx \n") ;
  44.   scanf ("%s" , buffer) ;
  45.   
  46.   if ( stringToAddr( buffer , &in) != 0 )
  47.      printf ("inet_aton success\n") ;    
  48.   else
  49.    goto error ;

  50.   if ( addrToString ( buffer , in ) == 0 )
  51.     goto success ;
  52.   else goto error ;

  53.   error :
  54.     printf ("main error") ;
  55.     goto success ;
  56.  success:
  57.     return 0 ;
  58. }
运行结果:
input ip address like xxx.xxx.xxx.xxx 
127.0.0.1
transfer 127.0.0.1 into 0x100007f 
inet_aton  success
transfer 0x100007f into 127.0.0.1 
阅读(3132) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~