C # get a list of local area network hosts

xiaoxiao2021-03-06  38

Transfer from: http: //blog.9cbs.net/nanfansky/archive/2005/01/13/251162.aspx// LAN hosts // search Date: 2005.01.12 // Author: nanfansky // Reference: http: // Blog.aspcool.com/jiezhi

Using system.drawing; using system.collections; using system.bomponentmodel; using system.data; using system.net; using system.threading;

A summary description of Namespace Windowlansearch {///

/// Form1. /// 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> // The required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent (); Initlanhost (); ProgressBarsearch.maximum = 255;

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// array initialization /// private vid initlanhost () {lanhost = new string [255, 2]; for (INT i = 0; i <255; i ) {lanhost [ I, 0] = ""; lanhost [i, 1] = "" "}}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// 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 Search"; this.button1.click = new system.eventhandler (t his.button1_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

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.Run (new form1 ());} private void button1_click (Object sender, system.eventargs e) {lansearch ();} /// /// LAN Search Event /// private void lansearch () {thread = new thread [255];

Threadstart threadmeth;

Thread threadProgress = New Threadstart (New ThreadSTARCH); threadprogress.start ();

String localhost = (DNS.GethOstName ()))). AddressList [0] .tostring (); // Local host IP address str = localhost.substring (0, localhost.lastIndexof ("."));

For (int I = 0; i <255; i ) // Establish 255 thread scan IP {threadMethod = New ThreadStart (LANSEARCHREADMETHED); 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 it is right, I feel like there is no effect {thread [ I] .Abort ();}}

Getlanhost (); Listlanhost ();} ///

/// Multi-thread search method /// private void LansearchthReadMethod () {int currently_i = convert.touint16 (thread.currentthread.name); / / Current Process Name iPaddress Scanip = ipaddress.parse (STR "." Convert.TOString (Currently_i 1)); // Get Scan IP Address iphostentry ScanHost = Null; ScanHost = DNS.GETHOSTBYADDRESS (SCANIP); // Get 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;

} ///

/// text box displays host name and IP list /// 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";}} /// / // listView1 Display Search Host /// Private Void Listlanhost () {listView1.view = view.list;

转载请注明原文地址:https://www.9cbs.com/read-69121.html

New Post(0)