#region 搜索ftp服務器地址
/// <summary>
/// 搜索ftp服務器
/// </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:根據ip地址做事
currentIp = NextIP(currentIp);
}
}
#region ip地址相關
/// <summary>
/// 將ip地址轉換為數字表示
/// </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>
/// 從一個IPAddress 獲取它的下一個 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