namespace Tcp常规扫描程序
{
public partial class Form1 : Form
{
public int connState=0;
public int portSum = 0;
string 主机str;
int 开始端口 { get { return int.Parse(tex开始端口.Text); } }
int 结束端口 { get { return int.Parse(tex结束端口.Text); } }
int 线程数 { get { return int.Parse(tex线程数.Text); } }
IPAddress ip地址;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void 开始bt_Click(object sender, EventArgs e)
{
connState = 0;
portSum = 0;
主机str = textBox1.Text;
try
{
ip地址 = IPAddress.Parse(主机str);
textBox1.Text = ip地址.ToString();
}
catch(Exception a)
{
textBox1.Focus();
MessageBox.Show("请输入正确的主句地址,改地址dns无法解析", "系统提示");
return;
}
ThreadPool.SetMaxThreads(线程数, 线程数);//设置最大线程数
for (int _线程数 = 开始端口; _线程数 <= 结束端口; _线程数++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(正常扫描), _线程数);
}
}
public void 正常扫描(object state)
{
Int32 port = (Int32)state;
string tMsg = "";
string 获取数据 = "";
connState++;
try
{
TcpClient tcp = new TcpClient();
tcp.Connect(ip地址, port);
portSum++;
tMsg = port.ToString() + "端口开放";
listBox1.Items.Add(tMsg);
Stream sm = tcp.GetStream();
sm.Write(Encoding.Default.GetBytes(tMsg.ToCharArray()), 0, tMsg.Length);
StreamReader sr = new StreamReader(tcp.GetStream(), Encoding.Default);
try
{
获取数据 = sr.ReadLine();
//这行失败,无法读取协议消息.将自动跳转到catch语句
if (获取数据.Length != 0)
{
tMsg = port.ToString() + "端口数据:" + 获取数据.ToString();
listBox1.Items.Add("接受到数据:" + tMsg);
}
}
catch (Exception a) { }
finally
{
sr.Close();
sm.Close();
tcp.Close();
Thread.Sleep(0);//制定标识应挂起改线程以使其他等待线程能够执行
}
}
catch (Exception a) { }
finally
{
Thread.Sleep(0);
//asyncOpsAreDone.Close();
}
}
}
}
网友评论