具体的原理相关的看下面的网址
http://blog.csdn.net/perddy/article/details/3137637
下面是C语言代码的实现:
-
#include <stdio.h>
-
-
#define _IPV4_VAL_(addr3,addr2,addr1,addr0) ((((unsigned long)addr3 << 24) & 0xFF000000) | (((unsigned long)addr2 << 16) & 0xFF0000) | (((unsigned long)addr1 << 8) & 0xFF00) | (((unsigned long)addr0) & 0xFF))
-
#define _IPV4_BYTE_(val, index) ((val >> (index * 8)) & 0xFF)
-
-
int main(int argc,char **argv)
-
{
-
int ret_val = -1;
-
unsigned char ip_addr[4] = {192,168,4,100};
-
unsigned char ip_mask_addr[4] = {255,255,255,0};
-
unsigned long ip_addr_ = _IPV4_VAL_(ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
-
unsigned long ip_mask_addr_ = _IPV4_VAL_(ip_mask_addr[0], ip_mask_addr[1], ip_mask_addr[2], ip_mask_addr[3]);
-
-
unsigned char brocast_ip_addr[4];
-
unsigned long brocast_ip_addr_ = _IPV4_VAL_(ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
-
int index = 0;
-
-
for(index = 0; index < 32; index++)
-
{
-
if(ip_mask_addr_ & (1 << index))
-
{
-
break;
-
}
-
brocast_ip_addr_ |= (1 << index);
-
}
-
-
brocast_ip_addr[0] = _IPV4_BYTE_(brocast_ip_addr_, 3);
-
brocast_ip_addr[1] = _IPV4_BYTE_(brocast_ip_addr_, 2);
-
brocast_ip_addr[2] = _IPV4_BYTE_(brocast_ip_addr_, 1);
-
brocast_ip_addr[3] = _IPV4_BYTE_(brocast_ip_addr_, 0);
-
-
printf("brocast_ip_addr:%d.%d.%d.%d\n", brocast_ip_addr[0], brocast_ip_addr[1], brocast_ip_addr[2], brocast_ip_addr[3]);
-
-
return 0;
-
}
阅读(681) | 评论(0) | 转发(0) |