using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private System.Timers.Timer am = new System.Timers.Timer();
public Form1()
{
InitializeComponent();
am.Interval = 1000;
am.Elapsed += new System.Timers.ElapsedEventHandler(am_Elapsed);
}
void am_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (tcp != null)
{
tcp.Close();
}
Thread.CurrentThread.Abort();
}
private static int i1, i2, i3, i4, d1,p1;
private void button1_Click(object sender, EventArgs e)
{
try
{
this.listView1.Clear();
this.listView1.Columns.Add("IP", 100, HorizontalAlignment.Left);
this.listView1.Columns.Add("端口"+this.textBox5.Text, 50, HorizontalAlignment.Left);
this.listView1.Columns.Add("端口信息", 150, HorizontalAlignment.Left);
i1 = int.Parse(this.textBox1.Text);
i2 = int.Parse(this.textBox2.Text);
i3 = int.Parse(this.textBox3.Text);
i4 = int.Parse(this.textBox4.Text);
d1 = int.Parse(textBox9.Text);
p1 = int.Parse(this.textBox5.Text);
if (i1 >= 255 || i2 >= 255 || i3 >= 255 || i4 >= 255||p1>65535)
{
MessageBox.Show("你的开始IP 有误或端口号错误");
return;
}
else
{
if (p1 == 80)
{
this.ds = " Connection: Closernrn";
}
else
{
this.ds = "";
}
this.progressBar1.Maximum = int.Parse(this.textBox9.Text);
this.progressBar1.Value = 1;
strat();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.listView1.Columns.Add("IP", 100, HorizontalAlignment.Left);
this.listView1.Columns.Add("端口"+this.textBox5.Text, 50, HorizontalAlignment.Left);
this.listView1.Columns.Add("端口信息", 150, HorizontalAlignment.Left);
}
Thread[] thread;
private void strat()
{
thread = new Thread[d1];
int s = d1 - 1;
while (true)
{
thread[s] = new Thread(new ThreadStart(dowhlies));
thread[s].Start();
s--;
if (s == 0)
break;
}
}
private string ips()
{
lock (this)
{
i4++;
if (i4 == 254)
{
i4 = 1;
i3++;
}
else if (i3 == 254)
{
i3 = 0;
i2++;
}
else if (i2 == 254)
{
i2 = 0;
i1++;
}
return i1.ToString() + "." + i2.ToString() + "." + i3.ToString() + "." + i4.ToString();
}
}
private int d = 0;
private int s = 1;
private string ds = "";
public AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false);
TcpClient tcp;
private void dowhlies()
{
string dis = ips();
string de = "";
//string ds = " Connection: Closernrn";
string getData="";
try
{
tcp = new TcpClient();
am.Start();
tcp.Connect(dis, p1);
am.Stop();
de = "开放";
Stream sm = tcp.GetStream();
am.Start();
sm.Write(Encoding.Default.GetBytes(ds.ToCharArray()), 0, ds.Length);
am.Stop();
StreamReader sr = new StreamReader(tcp.GetStream(), Encoding.Default);
am.Start();
getData = sr.ReadLine();
am.Stop();
if (getData.Length != 0)
{
ds = p1 + "端口数据:" + getData.ToString();
}
else
{
ds = "无端口数据";
}
sr.Close();
sm.Close();
tcp.Close();
d = d + 1;
label4.Text = "有" + d.ToString() + "主机获取端口成功..";
}
catch (IOException ed)
{
de = "开放";
ds = ed.Message;
d = d + 1;
label4.Text = "有" + d.ToString() + "主机获取端口成功..";
}
catch (NullReferenceException ee)
{
de = "开放";
ds = "目标装有防火墙";
d = d + 1;
label4.Text = "有" + d.ToString() + "主机获取端口成功..";
}
catch (Exception ex)
{
s = s + 1;
de = "关闭";
ds = ex.Message;
label5.Text = "有" + s.ToString() + "主机获取端口失败..";
}
ListViewItem l1 = new ListViewItem();
l1.SubItems[0].Text = dis;
l1.SubItems.Add(de);
l1.SubItems.Add(ds);
this.listView1.Items.Add(l1);
this.listView1.Update();
this.progressBar1.Value++;
asyncOpsAreDone.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.listView1.Clear();
this.d = 0;
this.s = 0;
}
private void button3_Click(object sender, EventArgs e)
{
if (thread != null)
{
for (int i = 0; i < thread.Length; i++)
{
if (tcp != null)
{
tcp.Close();
}
thread[i].Abort();
}
}
}
}
}