(轉)用C#實現實現簡單的 Ping 的功能,用於測試網絡是否已經連通

本文轉載自:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx
網絡

用C#實現實現簡單的 Ping 的功能,用於測試網絡是否已經聯通測試

1. 根據IP地址得到主機名稱this

 /// <summary>
        /// 根據IP地址得到主機名稱
        /// </summary>
        /// <param name="ip">主機的IP地址</param>
        /// <returns>主機名稱</returns>
        public string GetHostNameByIp(string ip)
        {
            ip = ip.Trim();
            if (ip == string.Empty)
                return string.Empty;
            try
            {
                // 是否 Ping 的通
                if (this.Ping(ip))
                {
                    System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(ip);
                    return host.HostName;
                }
                else
                    return string.Empty;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

說明:若是你的電腦能夠上網你甚至能夠查詢到:IP地址「64.233.189.104」是 Google 的一個名爲「hk-in-f104.google.com」的主機的IP地址。google

 

關於代碼中 this.Ping(ip) 方法後面再說。spa

既然說了如何「根據IP地址得到主機名稱」,那就要再說說如何「根據主機名得到主機的IP地址」吧。.net

2. 根據主機名得到主機的IP地址線程

 /// <summary>
        /// 根據主機名(域名)得到主機的IP地址
        /// </summary>
        /// <param name="hostName">主機名或域名</param>
        /// <example> GetIPByDomain("www.google.com");</example>
        /// <returns>主機的IP地址</returns>
        public string GetIpByHostName(string hostName)
        {
            hostName = hostName.Trim();
            if (hostName == string.Empty)
                return string.Empty;
            try
            {
                System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(hostName);
                return host.AddressList.GetValue(0).ToString();
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

說明:若是你的電腦能夠上網你甚至能夠查詢到:「www.google.com」的IP地址是「64.233.189.104」。code

最後,再說說C#實現簡單的 Ping 的功能,用於測試網絡是否已經聯通。orm

3. C#實現簡單的 Ping 的功能,用於測試網絡是否已經聯通blog

/// <summary>
        /// 是否能 Ping 通指定的主機
        /// </summary>
        /// <param name="ip">ip 地址或主機名或域名</param>
        /// <returns>true 通,false 不通</returns>
        public bool Ping(string ip)
        {
            System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
            System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
            options.DontFragment = true;
            string data = "Test Data!";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 1000; // Timeout 時間,單位:毫秒
            System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
            if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
                return true;
            else
                return false;
        }
private void button1_Click(object sender, EventArgs e)//獲取IP
        { textBox2.Text=GetIpByHostName(textBox1.Text);   }
  private void button2_Click(object sender, EventArgs e)//獲取主機名
        {  textBox1.Text=GetHostNameByIp(textBox2.Text);   }
   private void button3_Click(object sender, EventArgs e)//獲取本機IP
        {
            string hostName = System.Net.Dns.GetHostName();
            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName);
            //Dns.GetHostByName(myName);已過期
            IPAddress[] addr = ipEntry.AddressList;
            textBox2.Text = addr[0].ToString();
        }
        /// <summary>
        /// 局域網搜索事件
        /// </summary>
        private void LanSearch()
        {
string localhost = (Dns.GetHostEntry(Dns.GetHostName()))
.AddressList[0].ToString(); //本地主機IP地址
            string str = localhost.Substring(0, localhost.LastIndexOf("."));
            for (int i = 0; i < 255; i++) //創建255個線程掃描IP
            {
                string ip = str + "." + i.ToString();
                this.Text  = ip;
                if (Ping(ip))
                {
                    listBox1.Items.Add(ip);                       
                    Application.DoEvents(); 
                }
                progressBarSearch.Value = progressBarSearch.Value + 1;
            }
        }
        private void button4_Click(object sender, EventArgs e)//在線主機
        {
            LanSearch();
        }
相關文章
相關標籤/搜索