IP地址结构:
struct in_addr {
union {
struct {
unsigned char s_b1,
s_b2,
s_b3,
s_b4;
} S_un_b;
struct {
unsigned short s_w1,
s_w2;
} S_un_w;
unsigend long S_addr;
}S_un;
};
以上所指出的IP地址为IPV4,长度为32bit.unsigned char 为8bit,unsigned short 为16bit,unsigned long 为32bit.如果要表示一个IP地址:10.14.25.90依据上面的结构体,可以有3种不同的方式:假设定义结构体 in_addr sin_addr;
1.sin_addr.S_un.S_un_b.s_b1 = 10;
sin_addr.S_un.S_un_b.s_b1 = 14;
sin_addr.S_un.S_un_b.s_b1 = 25;
sin_addr.S_un.S_un_b.s_b1 = 90;
2.sin_addr.S_un.Sun_w.s_w1 = (14<<8)|10;
sin_addr.S_un.Sun_w.s_w2 = (90<<8)|25;
3.sin_addr.S_un.S_addr = (90<<24)|(25<<16)|(14<<8)|10;
没有评论:
发表评论