C# 如何telnet IP的某端口/ping 是否通

 //检查是否ping通
        private bool checkPingEnable(string _ip)
        {
            bool _isEnable = false;
            try
            {
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(_ip, 120);//第一个参数为ip地址,第二个参数为ping的时间 
                if (reply.Status == IPStatus.Success)
                {
                    _isEnable = true;
                }
                else
                {
                    _isEnable = false;
                }
            }
            catch (Exception)
            {

                _isEnable = false;
            }
            return _isEnable;
        }
  /// <summary>
        /// telnet port 
        /// </summary>
        /// <param name="_ip"></param>
        /// <param name="_port"></param>
        /// <returns></returns>
        private bool checkPortEnable(string _ip , int _port)
        {
            //将IP和端口替换成为你要检测的
            string ipAddress = _ip;
            int portNum = _port;
            IPAddress ip = IPAddress.Parse(ipAddress);
            IPEndPoint point = new IPEndPoint(ip, portNum);

            bool _portEnable = false;
            try
            {
                using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    sock.Connect(point);
                    //Console.WriteLine("连接{0}成功!", point);
                    sock.Close();

                    _portEnable = true;
                }
            }
            catch (SocketException e)
            {
                //Console.WriteLine("连接{0}失败", point);
                _portEnable = false;
            }
            return _portEnable;
        }

转自互联网:https://blog.csdn.net/gangli_8/article/details/117649561

原文地址:https://www.cnblogs.com/subendong/p/15244534.html