using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
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;
namespace CXHA._2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public static volatile float max = -1;
public static volatile float min = -1;
private static int flag = 0;
public MainWindow()
{
InitializeComponent();
label3.Visibility = Visibility.Hidden;
textBox.Text = "192.168.";
textBox1.Text = "1";
textBox2.Text = "3";
tb1.Text = "102";
tb2.Text = "105";
}
private void button_Click(object sender, RoutedEventArgs e)
{
string s1 = textBox.Text.ToString();
int start1 = int.Parse(textBox1.Text);
int end1 = int.Parse(textBox2.Text);
int start2 = int.Parse(tb1.Text);
int end2 = int.Parse(tb2.Text);
//起始值和终止值的差
int a1 = end1 - start1;
int a2 = end2 - start2;
//所有的地址个数
int length = (a1 + 1) * (a2 + 1);
if (a1<0 || a2<0 ) {
MessageBox.Show("起始值不能大于终止值,输入错误请更正!");
return;
}
listBox.Items.Clear();
for (int i = start1; i <= end1; i++) {//倒数第二个
for (int j = start2; j <= end2; j++)//倒数第一个
{
IPAddress ip;
if (textBox.Text.EndsWith(".") == false)
{
ip = IPAddress.Parse(textBox.Text +"."+ i + "." + j);
}
else
{
ip= IPAddress.Parse(textBox.Text + i + "." + j);
}
Thread t = new Thread(scan);
t.Start((object)ip);
}
}
Thread t1 = new Thread(show);
t1.Start((object)(length));
// show((object)(length));
}
public void scan(object ip)
{
Stopwatch w = Stopwatch.StartNew();
string host = "";
try
{
host = Dns.GetHostEntry((IPAddress)ip).HostName;
}
catch
{
host = "不在线";
}
w.Stop();
listBox.Dispatcher.Invoke(
() => listBox.Items.Add("扫描地址:" + ip + ",扫描用时:" + w.ElapsedMilliseconds + "毫秒,主机DNS名称:" + host)
);
if (flag == 0)
{
min = w.ElapsedMilliseconds;
max = w.ElapsedMilliseconds;
}
else
{
if (w.ElapsedMilliseconds >= max)
{
max = w.ElapsedMilliseconds;
}
if (w.ElapsedMilliseconds <= min)
{
min = w.ElapsedMilliseconds;
}
}
flag++;
}
public void show(object a)
{
while (true)
{
if (flag == int.Parse(a.ToString()))
{
label4.Dispatcher.Invoke(
() => label4.Content = "最大时间是:" + max
);
label5.Dispatcher.Invoke(
() => label5.Content = "最小时间是:" + min
);
MessageBox.Show("线程全部运行结束!");
flag = 0;
break;
}
}
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
string s1;
if (textBox.Text != "" && textBox1.Text != "" && textBox2.Text != "" && tb1.Text != "" && tb2.Text != "")
{
if (textBox.Text != "" && textBox.Text.EndsWith(".") == false)
{
s1 = textBox.Text + "."+textBox1.Text + tb1.Text;
}
else {
s1 = textBox.Text + textBox1.Text + tb1.Text;
}
string s2 = textBox.Text + textBox1.Text + tb2.Text;
IPAddress p1, p2;
if (IPAddress.TryParse(s1, out p1) == false || IPAddress.TryParse(s2, out p2) == false)
{
label3.Visibility = Visibility.Visible;
button.IsEnabled = false;
return;
}
else
{
label3.Visibility = Visibility.Hidden;
button.IsEnabled = true;
}
}else
{
button.IsEnabled = false;
}
}
}
}
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
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;
namespace CXHA._2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public static volatile float max = -1;
public static volatile float min = -1;
private static int flag = 0;
public MainWindow()
{
InitializeComponent();
label3.Visibility = Visibility.Hidden;
textBox.Text = "192.168.";
textBox1.Text = "1";
textBox2.Text = "3";
tb1.Text = "102";
tb2.Text = "105";
}
private void button_Click(object sender, RoutedEventArgs e)
{
string s1 = textBox.Text.ToString();
int start1 = int.Parse(textBox1.Text);
int end1 = int.Parse(textBox2.Text);
int start2 = int.Parse(tb1.Text);
int end2 = int.Parse(tb2.Text);
//起始值和终止值的差
int a1 = end1 - start1;
int a2 = end2 - start2;
//所有的地址个数
int length = (a1 + 1) * (a2 + 1);
if (a1<0 || a2<0 ) {
MessageBox.Show("起始值不能大于终止值,输入错误请更正!");
return;
}
listBox.Items.Clear();
for (int i = start1; i <= end1; i++) {//倒数第二个
for (int j = start2; j <= end2; j++)//倒数第一个
{
IPAddress ip;
if (textBox.Text.EndsWith(".") == false)
{
ip = IPAddress.Parse(textBox.Text +"."+ i + "." + j);
}
else
{
ip= IPAddress.Parse(textBox.Text + i + "." + j);
}
Thread t = new Thread(scan);
t.Start((object)ip);
}
}
Thread t1 = new Thread(show);
t1.Start((object)(length));
// show((object)(length));
}
public void scan(object ip)
{
Stopwatch w = Stopwatch.StartNew();
string host = "";
try
{
host = Dns.GetHostEntry((IPAddress)ip).HostName;
}
catch
{
host = "不在线";
}
w.Stop();
listBox.Dispatcher.Invoke(
() => listBox.Items.Add("扫描地址:" + ip + ",扫描用时:" + w.ElapsedMilliseconds + "毫秒,主机DNS名称:" + host)
);
if (flag == 0)
{
min = w.ElapsedMilliseconds;
max = w.ElapsedMilliseconds;
}
else
{
if (w.ElapsedMilliseconds >= max)
{
max = w.ElapsedMilliseconds;
}
if (w.ElapsedMilliseconds <= min)
{
min = w.ElapsedMilliseconds;
}
}
flag++;
}
public void show(object a)
{
while (true)
{
if (flag == int.Parse(a.ToString()))
{
label4.Dispatcher.Invoke(
() => label4.Content = "最大时间是:" + max
);
label5.Dispatcher.Invoke(
() => label5.Content = "最小时间是:" + min
);
MessageBox.Show("线程全部运行结束!");
flag = 0;
break;
}
}
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
string s1;
if (textBox.Text != "" && textBox1.Text != "" && textBox2.Text != "" && tb1.Text != "" && tb2.Text != "")
{
if (textBox.Text != "" && textBox.Text.EndsWith(".") == false)
{
s1 = textBox.Text + "."+textBox1.Text + tb1.Text;
}
else {
s1 = textBox.Text + textBox1.Text + tb1.Text;
}
string s2 = textBox.Text + textBox1.Text + tb2.Text;
IPAddress p1, p2;
if (IPAddress.TryParse(s1, out p1) == false || IPAddress.TryParse(s2, out p2) == false)
{
label3.Visibility = Visibility.Visible;
button.IsEnabled = false;
return;
}
else
{
label3.Visibility = Visibility.Hidden;
button.IsEnabled = true;
}
}else
{
button.IsEnabled = false;
}
}
}
}