如何遍历IP地址范围? [英] How can I loop through an IP address range?

查看:202
本文介绍了如何遍历IP地址范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在IP地址范围内执行一组网络任务.一旦范围变得比C类网络大,我就无法列举范围内的所有主机. 我希望能够使用网络掩码255.255.240.0遍历网络的所有主机.

I want to perform a set of network tasks on an IP address range. As soon as the range is getting bigger than a class c network, I fail to enumerate all hosts in the range. I want to be able to iterate through all hosts of a network with the netmask 255.255.240.0.


From: 192.168.0.100
To:   192.168.10.100

一个人将如何处理呢?这一定是很普通的任务.我来自可可iPhone编程的绿色领域,因此希望使用C风格的解决方案. :-)

How would one approach this? It must be a pretty common task. I come from the green fields of Cocoa iPhone programming, so a C-stylish solution would be appreciated. :-)

推荐答案

使用模运算符%.这是一个原始示例:

Use the modulus operator %. Here is a primitive example:

#include <stdio.h>

int main(void) {
    int counter;
    unsigned int ip[] = { 192, 168, 0, 0 };

    for ( counter = 0; counter < 1000; ++counter ) {
        ip[3] = ( ++ ip[3] % 256 );
        if ( !ip[3] ) {
            ip[2] = ( ++ ip[2] % 256 );
        }
        printf("%u:%u:%u:%u\n", ip[0], ip[1], ip[2], ip[3]);
    }

    return 0;
}

这篇关于如何遍历IP地址范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆