Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#添加到否<;价值>;左换环_C#_For Loop_Ip Address - Fatal编程技术网

C#添加到否<;价值>;左换环

C#添加到否<;价值>;左换环,c#,for-loop,ip-address,C#,For Loop,Ip Address,5在网络计算器上,输入用户输入的IP、子网以及所述主机IP的网络地址。(已计算) 我现在需要做的就是做一个向上计数的for循环,直到到达最后一个ip地址(广播) 所以, intip1=192,IP2=168,IP3=0,IP4=0,子网=255.255.0.0;//192.168.0.0/16 int totalIPs=65536//主机位^2 做 { 对于(int a=IP1;a

5在网络计算器上,输入用户输入的IP、子网以及所述主机IP的网络地址。(已计算)

我现在需要做的就是做一个向上计数的for循环,直到到达最后一个ip地址(广播)

所以,

intip1=192,IP2=168,IP3=0,IP4=0,子网=255.255.0.0;//192.168.0.0/16
int totalIPs=65536//主机位^2
做
{
对于(int a=IP1;a<255;a++)
{
IP1++;
对于(intb=IP2;b<255;b++)
{
IP2++;
对于(int c=IP3;c<255;c++)
{
IP3++;
对于(int d=IP4;d<255;d++)
{
totalIPs=totalIPs-1;
IP4++;
}
IP4=0;
}
IP3=0;
}
IP2=0;
}
}
而(totalIPs>0);
我知道这个for循环有严重的问题,但我似乎无法理解

我需要的是广播地址,从原始IP值(在本例中为192.168.0.0)开始计算,然后从那里计算到最后一个IP(现在我们知道它必须计算多少IP)


所以当八位位组4(d)达到255时,八位位组3(c)上升1,然后重置八位位组4(d)等等,就像时钟一样。

你真的不需要经历所有这些,你只需要在这里添加数字

首先你要这样做:

int carry = 0;
IP4 += totalIPs;

carry = IP4 / 254;
IP4 = IP4 % 254;

if(carry>0) // Need to spill over IP3
{
   IP3 += carry;

   carry = IP3 / 254;
   IP3 = IP3 % 254;

   if(carry > 0)  // Spill over to IP2
   {
       IP2 += carry;

       carry = IP2 / 254;
       IP2 = IP2 % 254;

       IP1 += carry;
       IP1 %= 254;    // No spill-over here
   }
}

这有点麻烦,因为您选择对IP部件使用4个整数,但它可以在没有循环的情况下完成任务。

这是您想要的吗

int IP1 = 192, IP2 = 168, IP3 = 0, IP4 = 0;

for (int i1 = IP1; i1 < 256; i1++) {
    for (int i2 = IP2; i2 < 256; i2++) {
        for (int i3 = IP3; i3 < 256; i3++) {
            for (int i4 = IP4; i4 < 256; i4++) {
                string ip = String.Format("{0}.{1}.{2}.{3}", i1 i2, i3, i4);
                ...
            }
            IP4 = 0;
        }
        IP3 = 0;
    }
    IP2 = 0;
}
intip1=192,IP2=168,IP3=0,IP4=0;
对于(int i1=IP1;i1<256;i1++){
对于(int i2=IP2;i2<256;i2++){
对于(int i3=IP3;i3<256;i3++){
对于(inti4=IP4;i4<256;i4++){
stringip=string.Format(“{0}.{1}.{2}.{3}”,i1i2,i3,i4);
...
}
IP4=0;
}
IP3=0;
}
IP2=0;
}

或者您可以依靠.NET Framework:

byte IP1 = 192, IP2 = 168, IP3 = 0, IP4 = 0;
int totalIPs = 65536;

int littleEndian = IP4 + (IP3 << 8) + (IP2 << 16) + (IP1 << 24) + totalIPs;
var bigEndian = BitConverter.GetBytes(littleEndian).Reverse().ToArray();
Console.WriteLine(new System.Net.IPAddress(bigEndian));
字节IP1=192,IP2=168,IP3=0,IP4=0;
int totalIPs=65536;

int littleEndian=IP4+(IP3,如果它实际上是a/16,你就不会想要0.0和255.255-但192.168通常是256/24的集合。[所以你想要192.168.{0-255}.{1-254}]嗯,这应该是一种方法,无论我插入什么输入?@zmbq抱歉,我的意思是它应该是255!这只会让程序“挂起”?您在最内部的循环中放置了什么?循环不会挂起,但是它们将迭代大约一百万次!您是否打算只循环最后两个八位字节?这将进行65536次迭代。嗯,我有
totalIPs-1;
:P我还需要什么来计算最后一个IP吗?我算出了,我停止了它一个无休止的循环,虽然我得到的是
192.0.0.0
作为最后一个IP地址,但它不是。不要使用
IP1
-
IP4
,而是
i1
-
i4
IP1
-
IP4
只是起始地址!在我的示例中,使用
String.Format
我使用
i1
-
i4
byte IP1 = 192, IP2 = 168, IP3 = 0, IP4 = 0;
int totalIPs = 65536;

int littleEndian = IP4 + (IP3 << 8) + (IP2 << 16) + (IP1 << 24) + totalIPs;
var bigEndian = BitConverter.GetBytes(littleEndian).Reverse().ToArray();
Console.WriteLine(new System.Net.IPAddress(bigEndian));