#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