u_long ulDestIP;
u_long ulDestIP1=inet_addr(a);
u_long ulDestIP2=inet_addr(b);
unsigned long count = ntohl(ulDestIP2 - ulDestIP1)-1;
struct in_addr in;
unsigned long hostip;
for(unsigned int i = 0; i <=count+1; i++)
{
hostip = ntohl(ulDestIP1) + i;
in.S_un.S_addr = htonl(hostip);
strcpy(IpAddress, inet_ntoa(in));
cout << "IP地址:"<<IpAddress<<"————————";
ulDestIP=inet_addr(IpAddress);
以下是分析思路 仅供参考 把问题拆解 加猜测 就会得到你想要的
//////////////////////////////////////////////////////////
u_long ulDestIP;
u_long ulDestIP1=inet_addr(a);//如果正确执行将返回一个无符号长整数型数。如果传入的字符串不是一个合法的IP地址,将返回INADDR_NONE。
u_long ulDestIP2=inet_addr(b);//如果正确执行将返回一个无符号长整数型数。如果传入的字符串不是一个合法的IP地址,将返回INADDR_NONE。
unsigned long count = ntohl(ulDestIP2 - ulDestIP1)-1;//ntohl()是将一个无符号长整形数从网络字节顺序转换为主机字节顺序。
struct in_addr in;
unsigned long hostip;
for(unsigned int i = 0; i <=count+1; i++)
{
hostip = ntohl(ulDestIP1) + i;//ntohl()是将一个无符号长整形数从网络字节顺序转换为主机字节顺序。
in.S_un.S_addr = htonl(hostip);//将主机数转换成无符号长整形的网络字节顺序。
strcpy(IpAddress, inet_ntoa(in));
cout << "IP地址:"<<IpAddress<<"————————";
ulDestIP=inet_addr(IpAddress);
}
如a 为192.168.1.1
b 为192.168.1.10
输出 192.168.1.1---192.168.1.10
输出在这俩个IP之间有多少个IP地址 输出这些IP地址