Chinaunix首页 | 论坛 | 博客
  • 博客访问: 351794
  • 博文数量: 82
  • 博客积分: 3353
  • 博客等级: 中校
  • 技术积分: 742
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-20 19:13
文章分类

全部博文(82)

文章存档

2015年(1)

2014年(1)

2013年(1)

2012年(12)

2011年(3)

2010年(25)

2009年(37)

2008年(2)

我的朋友

分类: 系统运维

2009-07-07 10:59:48

IP地址有两种表示方式:字符型的IP,二进制型的IP
   192.168.67.45 是主机上的,也是我们日常生活中所说的形式,即为字符型的,称为:numbers-and-dots notation 。
   192.168.67.45转换成二进制后,用%d打印出来就是:759408832,IP地址的二进制,称为: binary date 。
   处理IP地址的函数有:
inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof, inet_netof - Internet address manipulation routines 
SYNOPSIS

#include
#include
#include

int inet_aton(const char *cp, struct in_addr *inp);

in_addr_t inet_addr(const char *cp);

in_addr_t inet_network(const char *cp);

char *inet_ntoa(struct in_addr in);

struct in_addr inet_makeaddr(int net, int host);

in_addr_t inet_lnaof(struct in_addr in);

in_addr_t inet_netof(struct in_addr in);

 
DESCRIPTION
inet_aton() converts the Internet host address cp from the standard numbers-and-dots notation into binary data and stores it in the structure that inp points to. inet_aton returns nonzero if the address is valid, zero if not.

The inet_addr() function converts the Internet host address cp from numbers-and-dots notation into binary data in network byte order. If the input is invalid, INADDR_NONE (usually -1) is returned. This is an obsolete interface to inet_aton, described immediately above; it is obsolete because -1 is a valid address (255.255.255.255), and inet_aton provides a cleaner way to indicate error return.

The inet_network() function extracts the network number in host byte order from the address cp in numbers-and-dots notation. If the input is invalid, -1 is returned.

The inet_ntoa() function converts the Internet host address in given in network byte order to a string in standard numbers-and-dots notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite.

The inet_makeaddr() function makes an Internet host address in network byte order by combining the network number net with the local address host in network net, both in local host byte order.

The inet_lnaof() function returns the local host address part of the Internet address in. The local host address is returned in local host byte order.

The inet_netof() function returns the network number part of the Internet Address in. The network number is returned in local host byte order.

The structure in_addr as used in inet_ntoa(), inet_makeaddr(), inet_lnoaf() and inet_netof() is defined in netinet/in.h as:

    struct in_addr {
            unsigned long int s_addr;
    }
Note that on the i80x86 the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Significant Byte first.  

下面是我的测试的一些结果:
源码:
printf("socket  success!sockfd= %d \n",sockfd);               
                server_sockaddr.sin_family=AF_INET;
                server_sockaddr.sin_port=htons(SERVPORT);
                server_sockaddr.sin_addr.s_addr=INADDR_ANY;
                bzero(&(server_sockaddr.sin_zero),8);
                printf("the SERVPORT : %d\nthe sin_port : %d \n",SERVPORT,server_sockaddr.sin_port);               
                printf("the server IP si :%u\n ",server_sockaddr.sin_addr.s_addr);
执行结果:
[root@localhost 09-7-3]# ./t1
socket  success!sockfd= 3
the SERVPORT : 3333            -----》110100000101---》倒序得2571
  the sin_port : 1293            -----》10100001101
the server IP si :0
 binding is successful
linstening   ......

实际上:
  此处用htons()函数,是short int 类型的,所以把port表示成16位,00001101 00000101
将前后两个字节对换下位置,就成了:00000101 00001101
最前面的0省去,就成了对应的3333和1293
因为是以字节为单位的,所以,各种转换函数都是将字节的顺序转换,而不是安位顺序转换,从上例可看出。

注意:要放到网络通信函数里使用的量,所有IP、端口号等,都必须转换成网络字节序!

Internet网络上是以高位“字节”优先顺序在网络上传输的,即是所谓的big-endian模式
主机上,x86一般是以little-endian模式的

in_addr_t inet_ntoa(server_sockaddr.sin_addr),此处的参数必须是struct in_addr,
若用server_sockaddr.sin_addr.a_ddr,则出错;

源码:
printf("the IP is : %d\n",server_sockaddr.sin_addr.s_addr);
printf("the IP is :%s \n",inet_ntoa(server_sockaddr.sin_addr));
执行结果:
the IP is : 0
the IP is :0.0.0.0
说明:返回的是字符串,也就是in_addr_t,在不同场合,定义的是不同的。

源码:
n=inet_aton("192.168.67.45",&server_sockip);
printf("convert the 192.68.67.45 to binary date is:%d \n",server_sockip.s_addr);

执行结果:
convert the 192.168.67.45 to binary date is:759408832
将点数形式转换成二进制形式,且此二进制是以网络字节序表示的。

经过计算,可知:
   对于IP地址的形式转换(numbers-and-dots notation 与 binary data之间),转换 也是以字节为单位的。


源码:
n=inet_aton("192.168.67.45",&server_sockip);
printf("use inet_aton convert the 192.168.67.45 to binary date is:%d \n",server_sockip.s_addr);
printf("use inet_addr convert the 192.168.67.45 to binary data is:%d\n",
                                                        inet_addr("192.168.67.45"));
执行结果:
use inet_aton convert the 192.168.67.45 to binary date is:759408832
use inet_addr convert the 192.168.67.45 to binary data is:759408832


说明:
inet_aton()与inet_addr()执行的效果一样,均是将字符型的IP地址转换成二进制型的,且用网络字节序。
区别在于:两者参数、返回值不同;


源码:
printf("use inet_network convert the 192.168.67.45 to binary data is:%u\n",
                                      inet_network("192.168.67.45"));
执行结果:
use inet_network convert the 192.168.67.45 to binary data is:3232252717
说明:
与上面的inet_addr()的执行结果相比,经过计算,可知:
   3232252717就是192.168.67.45的二进制表示,是主机字节序的(host byte order).

注意:在系统函数中应用的


函数原型,在库里的:
/* Convert Internet host address from numbers-and-dots notation in CP
   into binary data in network byte order.  */
extern in_addr_t inet_addr (__const char *__cp) __THROW;

/* Extract the network number in network byte order from the address
   in numbers-and-dots natation starting at CP.  */ (此处的network byte order 有误) 
extern in_addr_t inet_network (__const char *__cp) __THROW;

源码:
//test inet_aton(const char *p, struct in_addr * ip)
    n=inet_aton("192.168.67.45",&server_sockip);
    printf("use inet_aton convert the 192.168.67.45 to binary date is:%u \n",server_sockip.s_addr);
    //test inet_addr(const char *p)
    printf("use inet_addr convert the 192.168.67.45 to binary data is:%u\n",
                                                                     inet_addr("192.168.67.45"));
    //test inet_network(const char *p )
    printf("use inet_network convert the 192.168.67.45 to binary data is:%u\n",
                                                                  inet_network("192.168.67.45"));

    //test inet_ntoa(struct in_addr ip)   
    sockip.s_addr=759408832 ;
    printf("use inet_ntoa convert the 759408832 to numbers-and-dots notationin iin host bytes order: %s \n",inet_ntoa(sockip));

执行结果:
use inet_aton convert the 192.168.67.45 to binary date is:759408832
use inet_addr convert the 192.168.67.45 to binary data is:759408832
use inet_network convert the 192.168.67.45 to binary data is:3232252717
use inet_ntoa convert the 759408832 to numbers-and-dots notationin iin host bytes order: 192.168.67.45

inet_network()是将字符型的IP地址转换成二进制的IP地址,且是主机字节序。

阅读(1329) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~