首页 > 代码库 > 根据IP和Mask获得子网地址

根据IP和Mask获得子网地址

根据IP和Mask获得子网地址

unsigned int IPToUInt(const char *pIp){    int a,b,c,d;    unsigned int addr=0;    if( sscanf(pIp,"%d.%d.%d.%d",&a,&b,&c,&d)!=4 )        return 0;    addr=a<<24;    addr|=b<<16;    addr|=c<<8;    addr|=d;    return addr;}

char* pIp,char *pMask

。。。

unsigned int ip = IPToUInt(pIp);
unsigned int mask= IPToUInt(pMask);
unsigned int net_lower=( ip & mask );
unsigned int net_upper=( net_lower | (~mask) );
unsigned long m_lsubnet=net_upper;

。。。

struct sockaddr_in subnet_addr;
subnet_addr.sin_family=AF_INET;
subnet_addr.sin_addr.s_addr=pLSearch->m_lsubnet;//inet_addr("255.255.255.255");//inet_addr(pLSearch->m_subip);
subnet_addr.sin_port=htons(BROADCAST_SEND_PORT_L);