这个版本的代码解决了跨网段的问题。但是会多计算出一个网段。
代码
///
<summary>
/// Get the next ip by given ip
/// </summary>
/// <param name="nowIP"> Given IP(Now IP) </param>
/// <returns> the next ip </returns>
static IPAddress NextIP(IPAddress nowIP)
{
byte [] ipBytes = nowIP.GetAddressBytes();
if (ipBytes[ 3 ] != 254 )
{
ipBytes[ 3 ] += 1 ;
}
else if (ipBytes[ 2 ] != 254 )
{
ipBytes[ 2 ] += 1 ;
ipBytes[ 3 ] = 1 ;
}
else if (ipBytes[ 1 ] != 254 )
{
ipBytes[ 1 ] += 1 ;
ipBytes[ 2 ] = 1 ;
ipBytes[ 3 ] = 1 ;
}
else if (ipBytes[ 0 ] != 254 )
{
ipBytes[ 0 ] += 1 ;
ipBytes[ 1 ] = 1 ;
ipBytes[ 2 ] = 1 ;
ipBytes[ 3 ] = 1 ;
}
else
{
// return null;
//
}
IPAddress ip = new IPAddress(ipBytes);
return ip;
}
/// Get the next ip by given ip
/// </summary>
/// <param name="nowIP"> Given IP(Now IP) </param>
/// <returns> the next ip </returns>
static IPAddress NextIP(IPAddress nowIP)
{
byte [] ipBytes = nowIP.GetAddressBytes();
if (ipBytes[ 3 ] != 254 )
{
ipBytes[ 3 ] += 1 ;
}
else if (ipBytes[ 2 ] != 254 )
{
ipBytes[ 2 ] += 1 ;
ipBytes[ 3 ] = 1 ;
}
else if (ipBytes[ 1 ] != 254 )
{
ipBytes[ 1 ] += 1 ;
ipBytes[ 2 ] = 1 ;
ipBytes[ 3 ] = 1 ;
}
else if (ipBytes[ 0 ] != 254 )
{
ipBytes[ 0 ] += 1 ;
ipBytes[ 1 ] = 1 ;
ipBytes[ 2 ] = 1 ;
ipBytes[ 3 ] = 1 ;
}
else
{
// return null;
//
}
IPAddress ip = new IPAddress(ipBytes);
return ip;
}