隐藏

C#实现通用对IP地址的检查是否合法与Ping指定IP是否畅通的

发布:2022/8/19 13:48:42作者:管理员 来源:本站 浏览次数:1276

一、实现功能

①实现检查IP地址、端口是否合法


②实现Ping指定的IP地址是否畅通


二、核心脚本

/***

* Title:"三维可视化" 项目

* 主题:【公共层】IP信息检查

* Description:

* 功能:

*    1、检查IP地址是否合法

*    2、检查端口是否合法

*    

* Date:2020

* Version:0.1版本

* Author:Coffee

* Modify Recoder:

*/


using System;

using System.Collections.Generic;

using System.Linq;

using System.Net.NetworkInformation;

using System.Text;

using System.Text.RegularExpressions;

using System.Threading;


namespace Global

{

   class IPInfoCheck

   {

       #region   基础参数



       #endregion



       #region   公有方法


       /// <summary>

       /// 匹配IP地址是否合法

       /// </summary>

       /// <param name="ip">当前需要匹配的IP地址</param>

       /// <returns>true:表示合法</returns>

       public static bool MatchIP(string ip)

       {

           bool success = false;

           if (!string.IsNullOrEmpty(ip))

           {

               //判断是否为IP

               success = Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");

           }

           return success;

       }


       /// <summary>

       /// 匹配端口是否合法

       /// </summary>

       /// <param name="port"></param>

       /// <returns>true:表示合法</returns>

       public static bool MatchPort(int port)

       {

           bool success = false;

           if (port >= 0 && port <= 65535)

           {

               success = true;

           }

           return success;

       }



       /// <summary>

       /// 检查IP是否可ping通

       /// </summary>

       /// <param name="strIP">要检查的IP</param>

       /// <returns>是否可连通【true:表示可以连通】</returns>

       public static bool CheckIPIsPing(string strIP)

       {

           if (!string.IsNullOrEmpty(strIP))

           {

               if (!MatchIP(strIP))

               {

                   return false;

               }

               // Windows L2TP VPN和非Windows VPN使用ping VPN服务端的方式获取是否可以连通

               Ping pingSender = new Ping();

               PingOptions options = new PingOptions();


               // 使用默认的128位值

               options.DontFragment = true;


               //创建一个32字节的缓存数据发送进行ping

               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);

           }

           else

           {

               return false;

           }

       }


       /// <summary>

       /// 连续几次查看是否某个IP可以PING通

       /// </summary>

       /// <param name="strIP">ping的IP地址</param>

       /// <param name="waitMilliSecond">每次间隔时间,单位:毫秒</param>

       /// <param name="testNumber">测试次数</param>

       /// <returns>是否可以连通【true:表示可以连通】</returns>

       public static bool MutiCheckIPIsPing(string strIP, int waitMilliSecond, int testNumber)

       {

           for (int i = 0; i < testNumber - 1; i++)

           {

               if (CheckIPIsPing(strIP))

               {

                   return true;

               }

               Thread.Sleep(waitMilliSecond);

           }


           return CheckIPIsPing(strIP);

       }


       #endregion


   }//Class_end

}

三、使用方法

①首先引用命名空间


using Global;

②使用方法示例:检查IP地址是否合法


//3-检查IP地址是否合法

bool isIp = IPInfoCheck.MatchIP(ip地址);

/比如我要检查192.168.1.1这个IP地址是否合法

bool isIp = IPInfoCheck.MatchIP("192.168.1.1");


③使用方法示例:检查IP是否可以Ping通


bool successful = IPInfoCheck.MutiCheckIPIsPing(ip地址,ping的间隔时间, Ping的次数);

//比如我要实现Ping 192.168.1.1这个地址3次,每次间隔100毫秒,查看是否在线,则示例如下:

successful = IPInfoCheck.MutiCheckIPIsPing("192.168.1.1",100, 3);