# Region Search ftp server address
/// <summary>
/// Search for an ftp server
/// </summary>
public void SearchFtpServer()
{
var beginIp=""192.168.1.1;
var endIp=""192.168.1.251;
var currentIp = IPAddress.Parse(beginIp);
var endIp = IPAddress.Parse(endIp);
var beginNum = GetIpNumber(currentIp);
var endNum = GetIpNumber(endIp);
for (long i = beginNum; i <= endNum; i++)
{
// TODO: work by IP Address
currentIp = NextIP(currentIp);
}
}
# Region IP Address
/// <summary>
/// Convert an IP address to a digital representation
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
private long GetIpNumber(IPAddress ip)
{
var ipBytes = ip.ToString().Split(new char[] { '.' });
var num = Convert.ToInt64(ipBytes[0]) * 256 * 256 * 256 +
Convert.ToInt64(ipBytes[1]) * 256 * 256 +
Convert.ToInt64(ipBytes[2]) * 256 +
Convert.ToInt64(ipBytes[3]);
return num;
}
/// <summary>
/// Obtain its next IPAddress from an IPAddress
/// </summary>
/// <param name="theIP"></param>
/// <returns></returns>
private IPAddress NextIP(IPAddress theIP)
{
byte[] ipBytes = theIP.GetAddressBytes();
if (ipBytes[3] != 255)
{
ipBytes[3] += 1;
}
else if (ipBytes[2] != 255)
{
ipBytes[2] += 1;
ipBytes[3] = 1;
}
else if (ipBytes[1] != 255)
{
ipBytes[1] += 1;
ipBytes[2] = 1;
ipBytes[3] = 1;
}
else if (ipBytes[0] != 255)
{
ipBytes[0] += 1;
ipBytes[1] = 1;
ipBytes[2] = 1;
ipBytes[3] = 1;
}
IPAddress ip = new IPAddress(ipBytes);
return ip;
}
#endregion