1 // LAN mainly search
2 / / Date: 2005.01.12
3 // Author: nanfansky
4 // Reference: http://blog.aspcool.com/jiezhi
5
6using system;
7using system.drawing;
8using system.collections;
9using system.componentmodel;
10using system.windows.forms;
11using system.data;
12USING System.net;
13using system.threading;
14
15namespace windowLanSearch
16 {
17 ///
18 /// Form1's summary description.
19 /// summary>
20 Public Class Form1: System.Windows.Forms.form
twenty one {
22 private system.windows.Forms.TextBox textBox1;
23 private system.windows.Forms.Button Button1;
24 private string [,] lanhost;
25 Private System.windows.Forms.ProgressBar Progressbarsearch;
26 private thread [] thread;
27 private system.windows.Forms.listview listview1;
28 private system.windows.forms.columnheader columnheader1;
29 private system.windows.Forms.columnheader columnheader2;
30 private string string Str;
31 ///
32 /// The required designer variable.
33 /// summary>
34 private system.componentmodel.container components = null;
35
36 Public Form1 ()
37 {
38 //
39 // Windows Form Designer Support
40 //
41 inTIAALIZEC MONENT ();
42 initlanhost ();
43 progressbarsearch.maximum = 255;
44
45 //
46 // Todo: Add any constructor code after INITIALIZECOMPONENT call
47 //
48}
49
50 ///
51 /// array initialization
52 /// summary>
53 private vidinitishost ()
54 {
55 lanhost = new string [255, 2];
56 for (int i = 0; i <255; i )
57 {
58 lanhost [i, 0] = ""
59 lanhost [i, 1] = ""
60}
61}
62
63 ///
64 /// Clean all the resources being used.
65 /// summary>
66 Protected Override Void Dispose (Bool Disposing)
67 {
68 IF (Disposing) 69 {
70 if (Components! = NULL)
71 {
72 Components.dispose ();
73}
74}
75 Base.Dispose (Disposing);
76}
77 #Region Windows Form Designer Generated Code ...
78
79 ///
80 /// Designer supports the required method - do not use the code editor to modify
81 /// The content of this method is.
82 /// summary>
83 private vidinitializeComponent ()
84 {
85 this.textBox1 = new system.windows.forms.textbox ();
86 this.button1 = new system.windows.Forms.Button ();
87 this.progressbarsearch = new system.windows.Forms.Progressbar ();
88 this.listview1 = new system.windows.Forms.listView ();
89 this.columnheader1 = new system.windows.Forms.columnhead ();
90 this.columnheader2 = new system.windows.Forms.columnhead ();
91 this.suspendlayout ();
92 //
93 // TextBox1
94 //
95 this.TextBox1.Location = new system.drawing.point (24, 40);
96 this.TextBox1.multiline = true;
97 this.TextBox1.name = "textbox1";
98 this.textbox1.scrollbars = system.windows.Forms.Scrollbars.both;
99 this.textBox1.size = new system.drawing.size (176, 296);
100 this.TextBox1.tabindex = 0;
101 this.TextBox1.text = "";
102 ///
103 // Button1
104 //
105 this.button1.location = new system.drawing.point (456, 40);
106 this.button1.name = "button1";
107 this.button1.tabindex = 1;
108 this.button1.text = "Start search";
109 this.button1.click = new system.eventhandler (this.button1_click);
110 //
111 // Progressbarsearch
112 //
113 this.progressbarsearch.location = new system.drawing.point (32, 360);
114 this.progressbarsearch.name = "progravelbarsearch";
115 this.progressbarsearch.size = new system.drawing.size (490, 24); 116 this.progressbarsearch.tabindex = 2;
117 //
118 // ListView1
119 //
120 this.listview1.columns.addrange (new system.windows.forms.columnheader [] {
121 this.columnheader1,
122 this.columnheader2});
123 this.listview1.location = new system.drawing.point (248, 40);
124 this.listview1.name = "listview1";
125 this.listview1.size = new system.drawing.size (184, 288);
126 this.listview1.tabindex = 5;
127 //
128 // ColumnHeader1
129 //
130 this.columnheader1.text = "dddd";
131 //
132 // ColumnHeader2
133 //
134 this.columnheader2.text = "sssss";
135 ////
136 // Form1
137 //
138 this.autoscalebasesize = new system.drawing.size (6, 14);
139 this.clientsize = new system.drawing.size (544, 413);
140 this.controls.add (this.listview1);
141 this.Controls.add (this.progressbarsearch);
142 this.Controls.add (this.button1);
143 this.Controls.add (this.textbox1);
144 this.name = "form1";
145 this.Text = "Form1";
146 this.ResumeLayout (FALSE);
147
148}
149 #ENDREGION
150
151 ///
152 /// The primary entry point of the application.
153 /// summary>
154 [stathread]
155 static void main ()
156 {
157 Application.run (New Form1 ());
158}
159 Private Void Button1_Click (Object Sender, System.EventArgs E)
160 {
161
162 lansearch ();
163
164}
165 ///
166 /// LAN Search Event
167 /// summary>
168 private void lansearch ()
169 {
170 thread = new three [255];
171
172 threadstart threadmeth;
173
174 Thread threadProgress = New ThreadSTARCH (New Threadstart (ProgressSearch)); 175 threadProgress.Start ();
176
177 string localhost = (DNS.GethostByname (DNS.GethOstName ()))). AddressList [0] .tostring (); // Local host IP address
178 str = localhost.substring (0, localhost.lastindexof ("."));
179
180 for (int i = 0; i <255; i ) // Establish 255 thread scan IP
181 {
182 threadMethod = New ThreadStart (LANSEARCHREADMETHOD);
183 Thread [I] = New Thread (ThreadMethod);
184 thread [i] .name = i.to.tostring ();
185 thread [i] .start ();
186 if (! Thread [i] .join (100)) //thread.join (100) I don't know if this is wrong, I feel that there is no effect.
187 {
188 thread [i] .Abort ();
189}
190}
191
192 getlanhost ();
193 Listlanhost ();
194}
195 ///
196 /// Multithreaded search method
197 /// summary>
198 Private Void LanSearchthreadMethod ()
199 {
200 int currently_i = convert.touint16 (thread.currentthread.name); // Current process name
201
202 ipaddress scanip = ipaddress.parse (str "." Convert.TOString (Currently_i 1)); // Get scan IP addresses
203 iphostentry scanhost = NULL;
204 scanhost = dns.gethostbyaddress (scanip); // get the scan IP address host information
205
206 IF (ScanHost! = NULL)
207 {
208 lanhost [currently_i, 0] = scanip.tostring ();
209 lanhost [currently_i, 1] = scanhost.hostname;
210}
211
212 //progressbarsearch.value = progressbarsearch.Value 1;
213
214}
215 ///
216 /// Text Box Displays the host name and IP list
217 /// summary>
218 private void getlanhost ()
219 {
220 for (int i = 0; i <255; i )
221 IF (Lanhost [i, 0]! = "")
222 {
223 textBox1.text = textbox1.text lanhost [i, 1] ":" lanhost [i, 0] "/ r / n"; 224}
225}
226 ///
227 /// ListView1 Display Search Host
228 /// summary>
229 Private Void Listlanhost ()
230 {
231 listView1.view = view.list;
232
233 ListViewItem AA;
234 for (int i = 0; i <255; i )
235 {
236 IF (Lanhost [i, 0]! = "")
237 {
238 aa = new listviewItem ();
239 aa.text = lanhost [i, 1];
240 aa.tag = lanhost [i, 0];
241 ListView1.Items.Add (AA);
242}
243}
244
245}
246 ///
247 /// progress bar processing thread
248 /// summary>
249 Private Void ProgressSearch ()
250 {
251 //label1.text = "The progress bar is only the time estimate, not real search progress!";
252 progressbarsearch.value = 0;
253 for (int i = 0; i <255; i )
254 {
255 progressbarsearch.value = progressbarsearch.value 1;
256 thread.sleep (100);
257}
258}
259}
260}
261
262 Regret: Due to the slow search, true search scheme is not realized.
263 Do not understand: When you implement a text prompt, inserting the first tail at the mouse event
264Private Void Button1_Click (Object Sender, System.EventArgs E)
265 {
266 lab1.text = "Start Search"; // New Insert
267 lansearch ();
268 lab1.text = "End Search"; // New Insert
269}
270 Text prompts, always display "End Search" on LAB1 is always displayed on LAB1, but "End Search" is displayed after all threads.
271 Beginner Winform programming, too much to learn.
272 hopes high pointing