C # obtain the LAN host list instance

Source: Internet
Author: User

Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Using System. Net;
Using System. Threading;

Namespace WindowLanSearch
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{
Private System. Windows. Forms. TextBox textBox1;
Private System. Windows. Forms. Button button1;
Private string [,] LanHost;
Private System. Windows. Forms. ProgressBar progressBarSearch;
Private Thread [] thread;
Private System. Windows. Forms. ListView listView1;
Private System. Windows. Forms. ColumnHeader columnHeader1;
Private System. Windows. Forms. ColumnHeader columnHeader2;
Private string str;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
InitLanHost ();
ProgressBarSearch. Maximum = 255;

//
// TODO: add Any constructor code after InitializeComponent calls
//
}

/// <Summary>
/// Array Initialization
/// </Summary>
Private void InitLanHost ()
{
LanHost = new string [255, 2];
For (int I = 0; I <255; I ++)
{
LanHost [I, 0] = "";
LanHost [I, 1] = "";
}
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. textBox1 = new System. Windows. Forms. TextBox ();
This. button1 = new System. Windows. Forms. Button ();
This. progressBarSearch = new System. Windows. Forms. ProgressBar ();
This. listView1 = new System. Windows. Forms. ListView ();
This. columnHeader1 = new System. Windows. Forms. ColumnHeader ();
This. columnHeader2 = new System. Windows. Forms. ColumnHeader ();
This. SuspendLayout ();
//
// TextBox1
//
This. textBox1.Location = new System. Drawing. Point (24, 40 );
This. textBox1.Multiline = true;
This. textBox1.Name = "textBox1 ";
This. textBox1.ScrollBars = System. Windows. Forms. ScrollBars. Both;
This. textBox1.Size = new System. Drawing. Size (176,296 );
This. textBox1.TabIndex = 0;
This. textBox1.Text = "";
//
// Button1
//
This. button1.Location = new System. Drawing. Point (456, 40 );
This. button1.Name = "button1 ";
This. button1.TabIndex = 1;
This. button1.Text = "start searching ";
This. button1.Click + = new System. EventHandler (this. button#click );
//
// ProgressBarSearch
//
This. progressBarSearch. Location = new System. Drawing. Point (32,360 );
This. progressBarSearch. Name = "progressBarSearch ";
This. progressBarSearch. Size = new System. Drawing. Size (490, 24 );
This. progressBarSearch. TabIndex = 2;
//
// ListView1
//
This. listView1.Columns. AddRange (new System. Windows. Forms. ColumnHeader [] {
This. columnHeader1,
This. columnHeader2 });
This. listView1.Location = new System. Drawing. Point (248, 40 );
This. listView1.Name = "listView1 ";
This. listView1.Size = new System. Drawing. Size (184,288 );
This. listView1.TabIndex = 5;
//
// ColumnHeader1
//
This. columnHeader1.Text = "dddd ";
//
// ColumnHeader2
//
This. columnHeader2.Text = "sssss ";
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (544,413 );
This. Controls. Add (this. listView1 );
This. Controls. Add (this. progressBarSearch );
This. Controls. Add (this. button1 );
This. Controls. Add (this. textBox1 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. ResumeLayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void button#click (object sender, System. EventArgs e)
{

LanSearch ();

}
/// <Summary>
/// LAN search event
/// </Summary>
Private void LanSearch ()
{
Thread = new Thread [2, 255];

ThreadStart threadMethod;

Thread threadProgress = new Thread (new ThreadStart (progressSearch ));
ThreadProgress. Start ();

String localhost = (Dns. GetHostByName (Dns. GetHostName (). AddressList [0]. ToString (); // local host IP Address
Str = localhost. Substring (0, localhost. LastIndexOf ("."));

For (int I = 0; I <255; I ++) // create 255 threads to scan IP addresses
{
ThreadMethod = new ThreadStart (LanSearchThreadMethod );
Thread [I] = new Thread (threadMethod );
Thread [I]. Name = I. ToString ();
Thread [I]. Start ();
If (! Thread [I]. Join (100) // Thread. Join (100) I don't know if this is the case, right?
{
Thread [I]. Abort ();
}
}

GetLanHost ();
ListLanHost ();
}
/// <Summary>
/// Multi-thread Search Method
/// </Summary>
Private void LanSearchThreadMethod ()
{
Int Currently_ I = Convert. ToUInt16 (Thread. CurrentThread. Name); // The Name of the current process.

IPAddress ScanIP = IPAddress. Parse (str + "." + Convert. ToString (Currently_ I + 1); // obtain the scan IP Address
IPHostEntry ScanHost = null;
ScanHost = Dns. GetHostByAddress (ScanIP); // obtain scan IP address host information

If (ScanHost! = Null)
{
LanHost [Currently_ I, 0] = ScanIP. ToString ();
LanHost [Currently_ I, 1] = ScanHost. HostName;
}

// ProgressBarSearch. Value = progressBarSearch. Value + 1;

}
/// <Summary>
/// The Host Name and IP address list are displayed in the text box.
/// </Summary>
Private void GetLanHost ()
{
For (int I = 0; I <255; I ++)
If (LanHost [I, 0]! = "")
{
TextBox1.Text = textBox1.Text + LanHost [I, 1] + ":" + LanHost [I, 0] + "\ r \ n ";
}
}
/// <Summary>
/// Listview1 Display Search host
/// </Summary>
Private void listLanHost ()
{
ListView1.View = View. List;

ListViewItem aa;
For (int I = 0; I <255; I ++)
{
If (LanHost [I, 0]! = "")
{
Aa = new ListViewItem ();
Aa. Text = LanHost [I, 1];
Aa. Tag = LanHost [I, 0];
ListView1.Items. Add (aa );
}
}

}
/// <Summary>
/// Progress bar processing thread
/// </Summary>
Private void progressSearch ()
{
// Label1.Text = "the progress bar is only a time estimation, not a real search progress! ";
ProgressBarSearch. Value = 0;
For (int I = 0; I <255; I ++)
{
ProgressBarSearch. Value = progressBarSearch. Value + 1;
Thread. Sleep (100 );
}
}
}
}

Sorry: because the search is slow, the actual search progress is not achieved.
Do not understand: When text prompts are implemented, insert at the beginning and end of the mouse event
Private void button#click (object sender, System. EventArgs e)
{
Lab1.Text = "Start search"; // new insert
LanSearch ();
Lab1.Text = "End search"; // new insert
}
When prompted in text, lab1 cannot be displayed in time, but the "End search" is displayed only after all threads are finished.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.