为了实现对IP地址后两段的任意设定 几乎改掉了之前所有的代码 -.-||
与之前http://blog.youkuaiyun.com/qq_35252878/article/details/60781383相比,增加了对IP地址后两位也可以设置最大最小值的功能。
效果:
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
namespace A._2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
long minTime;
long maxTime;
string add2_41;
int add2_4;
string add2_51;
int add2_5;
string add3_41;
int add3_4;
string add3_51;
int add3_5;
string add2;
string address;
string add;
private void judge_IPAddress(object Address)
{
string Dname;
String address=(string)Address;
Stopwatch sw = new Stopwatch();
sw.Start();
try
{
IPAddress ip = IPAddress.Parse(address);
Dname = Dns.GetHostEntry(ip).HostName;
}
catch
{
Dname = "(不在线)";
}
sw.Stop();
if (sw.ElapsedMilliseconds > maxTime)
maxTime = sw.ElapsedMilliseconds;
if (sw.ElapsedMilliseconds < minTime)
minTime = sw.ElapsedMilliseconds;
ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("扫描地址:" + Address + ",扫描用时:" + sw.ElapsedMilliseconds + "毫秒,主机DNS名称:" + Dname); }));
// ListBox.Items.Add("扫描地址:" + Address + ",扫描用时:"+sw.ElapsedMilliseconds+"毫秒,主机DNS名称:"+Dname);
//if(num==0)
// ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("扫描结束!"); }));
//Thread.Sleep(1000);
}
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void convrt(int star)
{
if(star==add2_4)
{
//MessageBox.Show("star1=" + star+"开始"+add2_5);
for(int k=add2_5;k<=255;k++)
{
add2 = star.ToString() + "." + k.ToString();
address = add + add2;
begin();
}
}
else if(star==add3_4)
{
for(int h=0;h<=add3_5;h++)
{
add2 = star.ToString() + "." + h.ToString();
address = add + add2;
begin();
}
}
else
{
//MessageBox.Show("star="+star);
for(int q=0;q<=255;q++)
{
add2 = star.ToString() + "." + q.ToString();
address = add + add2;
begin();
}
}
}
private void begin()
{
try
{
IPAddress ip = IPAddress.Parse(address);
}
catch
{
Textbox.Visibility = Visibility.Visible;
return;
}
Thread t = new Thread(judge_IPAddress);
t.Start(address);
}
private void button_Click(object sender, RoutedEventArgs e)
{
minTime = 0xffff;
maxTime = 0;
int flag = 0;
int the2 = 0;
int the3 = 0;
int L1 = 0;
int L2 = 0;
add = textBox.Text;
int l = add.Length;
if(add[l-1]!='.')
{
add = add + '.';
}
Textbox.Foreground = new SolidColorBrush(Colors.White);
//num = Int32.Parse(textBox2.Text) - Int32.Parse(textBox1.Text);
string add2 = textBox1.Text;
string add3 = textBox2.Text;
for (int i = 0; i < add2.Length;i++) //判断textbox1中是否是地址的后两段
{
if(add2[i]=='.')
{
flag = 1;
the2 = i;
}
}
if(flag==1) //起始值为***.***
{
L1 = add2.Length;
L2 = add3.Length;
add2_41 = add2.Substring(0, the2);//记录下倒数第二位的初始值
add2_4 = (Int32.Parse(add2_41)); //将倒数第二位的初始值转化int型
add2_51 = add2.Substring(L1 - the2);
add2_5 = Int32.Parse(add2_51);
for (int i = 0; i < add3.Length; i++)
{
if (add3[i] == '.')
the3 = i;
}
add3_41 = add3.Substring(0, the3);//记录下倒数第二位的结束值
add3_4 = (Int32.Parse(add3_41)); //将倒数第二位的结束值转化int型
add3_51 = add3.Substring(L2 - the3);
add3_5 = Int32.Parse(add3_51);
if (add2_4 > add3_4)
add2_4 ^= add3_4 ^= add2_4 ^= add3_4;
//ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("初始:" + add2_4 + ",结束:" + add3_4 + "初始:" + add2_5+"结束:"+add3_5); }));
for(int i=add2_4;i<=add3_4;i++)
{
convrt(i);
}
//ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("最长用时:" + maxTime + "s,最短用时:" + minTime + "s."); }));
MessageBox.Show("1扫描结束!"+"最长用时:"+maxTime+"最短用时:"+minTime);
}
if(flag==0) //起始值为***
{
for (int i = Int32.Parse(textBox1.Text); i <= Int32.Parse(textBox2.Text); i++)
{
address = add + i.ToString();
begin();
}
//t.Abort();
//ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("最长用时:" + maxTime + "s,最短用时:" + minTime + "s."); }));
MessageBox.Show("2扫描结束!" + "最长用时:" + maxTime + "最短用时:" + minTime);
}
}
private void Textbox_TextChanged_1(object sender, TextChangedEventArgs e)
{
}
}
}
目前只是达到了预期的效果,还有很多地方不尽完善。
一会或许还要进行完善,注释显示了很多思考过程,不想删。