獲得區域網路中IP和電腦名稱

來源:互聯網
上載者:User

本程式中採用多線程搜尋方法.獲得區域網路網段為"192.168.0.1"中已開機的IP和電腦名稱,並分別顯示在tB1和tB2兩個文字框中.具體的方法如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
namespace LanHostSerach
{
    public partial class Form1 : Form
    {
        private Thread[] thread;
        private string str = "192.168.0";
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 線程搜尋
        /// </summary>
        private void SearchLink()
        {
           int i = Convert.ToUInt16(Thread.CurrentThread.Name);
           IPAddress ScanIP = IPAddress.Parse(str + '.' + i.ToString());  //獲得掃描IP地址
            IPHostEntry ScanHost = null;
            try
            {
                ScanHost = Dns.GetHostByAddress(ScanIP);   //獲得掃描IP地址主機資訊 
            }
            catch (Exception E)
            {
            }
            if (ScanHost != null)
            {
                tB1.Text = tB1.Text + ScanHost.HostName + ":" + ScanIP.ToString() + "\r\n";
                tB2.Text = tB2.Text + ScanHost.HostName + "\r\n";
            }
        }
      private void button1_Click(object sender, EventArgs e)
        {
             thread = new Thread[255];
              ThreadStart threadMethod;
              for (int i = 0; i < 255; i++)  //建立255個線程掃描IP
              {
                  threadMethod = new ThreadStart(SearchLink);
                  thread[i] = new Thread(threadMethod);
                  thread[i].Name = i.ToString();
                  thread[i].Start();
                  if (!thread[i].Join(100))   
                  {
                      thread[i].Abort();
                  }
              }              
         }
  }

}
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.