C # Get LAN host list instance

Source: Internet
Author: User
Tags join thread tostring
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 description of the 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>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
Initlanhost ();
Progressbarsearch.maximum = 255;

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<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>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents 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 Search";
This.button1.Click + = new System.EventHandler (This.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

<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private void Button1_Click (object sender, System.EventArgs e)
{

Lansearch ();

}
<summary>
LAN Search Events
</summary>
private void Lansearch ()
{
thread = new thread[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 scan IP
{
Threadmethod = new ThreadStart (Lansearchthreadmethod);
Thread[i] = new Thread (Threadmethod);
Thread[i]. Name = i.ToString ();
Thread[i]. Start ();
if (!thread[i). Join (M))//thread.join (100) I don't know if this is the right thing to do, it doesn't feel good.
{
Thread[i]. Abort ();
}
}

Getlanhost ();
Listlanhost ();
}
<summary>
Multi-Threaded Search method
</summary>
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;

}
<summary>
text box displays host name and IP list
</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 = "Progress bar is only time estimate, not real search progress!" ";
Progressbarsearch.value = 0;
for (int i=0;i<255;i++)
{
Progressbarsearch.value = Progressbarsearch.value + 1;
Thread.Sleep (100);
}
}
}
}

Regret: Due to slow search, no real search progress has been achieved.
Do not understand: When implementing text prompts, when the mouse event in the end of the insertion
private void Button1_Click (object sender, System.EventArgs e)
{
Lab1. Text = "Start Search"; New insert
Lansearch ();
Lab1. Text = "End Search"; New insert
}
When the text is prompted, the LAB1 is never displayed in time, but the end search is not displayed until all threads are finished.
Beginner WinForm programming, too much to learn.
Hope Master Guide Twos



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.