c#實現驗證某個IP地址是否能ping通

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.NetworkInformation;
using System.Threading;
namespace Manager.Common
{
public static class NetCommon{
        /// <summary>
        /// 驗證IP地址字串的正確性
        /// </summary>
        /// <param name="strIP">要驗證的IP地址字串</param>
        /// <returns>格式是否正確,正確為 true 否則為 false</returns>
        public static bool CheckIPAddr(string strIP)
        {
            if (string.IsNullOrEmpty(strIP))
            {
                return false;
            }
            // 保持要返回的資訊
            bool bRes = true;
            int iTmp = 0;    // 保持每個由“.”分隔的整型
            string[] ipSplit = strIP.Split('.');
            if (ipSplit.Length < 4 || string.IsNullOrEmpty(ipSplit[0]) ||
                string.IsNullOrEmpty(ipSplit[1]) ||
                string.IsNullOrEmpty(ipSplit[2]) ||
                string.IsNullOrEmpty(ipSplit[3]))
            {
                bRes = false;
            }
            else
            {
                for (int i = 0; i < ipSplit.Length; i++)
                {
                    if (!int.TryParse(ipSplit[i], out iTmp) || iTmp < 0 || iTmp > 255)
                    {
                        bRes = false;
                        break;
                    }
                }
            }

            return bRes;
        }
        /// <summary>
        /// 驗證某個IP是否可ping通
          /// </summary>
        /// <param name="strIP">要驗證的IP</param>
        /// <returns>是否可連通  是:true 否:false</returns>
        public static bool TestNetConnectity(string strIP)
        {
            if (!NetUtil.CheckIPAddr(strIP))
            {
                return false;
            }
            // Windows L2TP VPN和非Windows VPN使用ping VPN服務端的方式擷取是否可以連通
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "testtesttesttesttesttesttesttest";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;
            PingReply reply = pingSender.Send(strIP, timeout, buffer, options);

            return (reply.Status == IPStatus.Success);
        }
        /// <summary>
        /// 連續幾次查看是否某個IP可以PING通
        /// </summary>
        /// <param name="strIP">ping的IP地址</param>
        /// <param name="WaitSecond">每次間隔時間,單位:秒</param>
        /// <param name="iTestTimes">測試次數</param>
        /// <returns>是否可以連通</returns>
        public static bool TestNetConnected(string strIP, int WaitSecond, int iTestTimes)
        {
            for (int i = 0; i < iTestTimes - 1; i++)
            {
                if (TestNetConnectity(strIP))
                {
                    return true;
                }
                Thread.Sleep(WaitSecond * 1000);
            }

            return TestNetConnectity(strIP);
        }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.