一个端口扫描的小程序
今天有个朋友让我帮他做个扫描端口的小程序,就是看某台机器打开了哪些端口,既然已经写了就顺便贴出来一下,虽然不是什么复杂东西,给相关需要的人参考一下吧:)下面是原代码
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using System.Threading;
9using System.Net;
10using System.Net.Sockets;
11
12namespace Scan
13{
14 public partial class Main : Form
15 {
16 //主线程
17 private Thread tdm;
18 //开始端口,结束端口
19 int Start, End;
20 //线程数
21 private int count=0;
22 //当前扫描端口
23 private int i;
24 public Main()
25 {
26 InitializeComponent();
27 }
28 /// <summary>
29 /// 副线程
30 /// </summary>
31 private void Process()
32 {
33 //当前要扫描的IP地址
34 string IP;
35
36 IP = mkIP.Text;
37
38 TcpClient tc = new TcpClient();
39 tc.SendTimeout = tc.ReceiveTimeout = 20000;
40
41 try
42 {
43 //建立连接
44 tc.Connect(IP, i);
45 //判断是否连接成功
46 if (tc.Connected)
47 {
48 tbDetail.Text = tbDetail.Text + "端口" + i.ToString() + "开放。\r\n";
49 }
50
51 }
52 catch (Exception ex)
53 {
54
55 }
56 finally
57 {
58 tc.Close();
59 tc = null;
60 //修改线程数
61 count--;
62//写入进度
63 pbProcess.Value = Convert.ToInt32((Convert.ToDouble(i) / Convert.ToDouble(End)) * 100);
64 }
65
66
67
68 }
69
70 private void button1_Click(object sender, EventArgs e)
71 {
72 Thread tmain = new Thread(new ThreadStart(MainProcess));
73 tmain.Start();
74
75 }
76 //主线程
77 private void MainProcess()
78 {
79 count = 0;
80 pbProcess.Value = 0;
81 tbDetail.Text = "";
82 Start = Convert.ToInt32(tbStar.Text);
83 End = Convert.ToInt32(tbEnd.Text);
84 //循环取副线程扫描端口
85 for (i = Start; i < End; i++)
86 {
87 count++;
88
89 tdm = new Thread(new ThreadStart(Process));
90 tdm.Start();
91 Thread.Sleep(10);
92 //线程数控制在20
93 while (count > 20) ;
94 }
95 while (count>0) ;
96 tbDetail.Text = tbDetail.Text+"扫描完成";
97 }
98 private void Main_Load(object sender, EventArgs e)
99 {
100 Control.CheckForIllegalCrossThreadCalls = false;
101 }
102
103
104 }
105
106}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using System.Threading;
9using System.Net;
10using System.Net.Sockets;
11
12namespace Scan
13{
14 public partial class Main : Form
15 {
16 //主线程
17 private Thread tdm;
18 //开始端口,结束端口
19 int Start, End;
20 //线程数
21 private int count=0;
22 //当前扫描端口
23 private int i;
24 public Main()
25 {
26 InitializeComponent();
27 }
28 /// <summary>
29 /// 副线程
30 /// </summary>
31 private void Process()
32 {
33 //当前要扫描的IP地址
34 string IP;
35
36 IP = mkIP.Text;
37
38 TcpClient tc = new TcpClient();
39 tc.SendTimeout = tc.ReceiveTimeout = 20000;
40
41 try
42 {
43 //建立连接
44 tc.Connect(IP, i);
45 //判断是否连接成功
46 if (tc.Connected)
47 {
48 tbDetail.Text = tbDetail.Text + "端口" + i.ToString() + "开放。\r\n";
49 }
50
51 }
52 catch (Exception ex)
53 {
54
55 }
56 finally
57 {
58 tc.Close();
59 tc = null;
60 //修改线程数
61 count--;
62//写入进度
63 pbProcess.Value = Convert.ToInt32((Convert.ToDouble(i) / Convert.ToDouble(End)) * 100);
64 }
65
66
67
68 }
69
70 private void button1_Click(object sender, EventArgs e)
71 {
72 Thread tmain = new Thread(new ThreadStart(MainProcess));
73 tmain.Start();
74
75 }
76 //主线程
77 private void MainProcess()
78 {
79 count = 0;
80 pbProcess.Value = 0;
81 tbDetail.Text = "";
82 Start = Convert.ToInt32(tbStar.Text);
83 End = Convert.ToInt32(tbEnd.Text);
84 //循环取副线程扫描端口
85 for (i = Start; i < End; i++)
86 {
87 count++;
88
89 tdm = new Thread(new ThreadStart(Process));
90 tdm.Start();
91 Thread.Sleep(10);
92 //线程数控制在20
93 while (count > 20) ;
94 }
95 while (count>0) ;
96 tbDetail.Text = tbDetail.Text+"扫描完成";
97 }
98 private void Main_Load(object sender, EventArgs e)
99 {
100 Control.CheckForIllegalCrossThreadCalls = false;
101 }
102
103
104 }
105
106}