1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Threading;
9
using System.Net;
10
using System.Net.Sockets;
11
12
namespace 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106
