示例#1
0
        public static bool TelnetPort(string ip, int port)
        {
            bool isOpen = false;

            try
            {
                IPAddress  ip1   = IPAddress.Parse(ip);
                IPEndPoint ipend = new IPEndPoint(ip1, port);
                TimeOutSocket.Connect(ipend, 500);
                if (TimeOutSocket.IsConnectionSuccessful)
                {
                    isOpen = true;
                }
            }
            catch (Exception)
            {
                return(false);
            }

            //Socket sock = null;
            //IPAddress ip1 = IPAddress.Parse(ip);

            //try
            //{

            //    sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //    sock.Connect(ip1, port);
            //    IAsyncResult connResult = sock.BeginConnect(ip, port, null, null);

            //    connResult.AsyncWaitHandle.WaitOne(2000, true);
            //    if (connResult.IsCompleted)
            //    {
            //        isOpen = true;
            //    }
            //    sock.Close();
            //}
            //catch (SocketException e)
            //{
            //    sock.Close();
            //}
            return(isOpen);
        }
示例#2
0
        public static bool TelnetPort(string ip, int port)
        {
            bool isOpen = false;

            try
            {
                IPAddress  ip1   = IPAddress.Parse(ip);
                IPEndPoint ipend = new IPEndPoint(ip1, port);
                TimeOutSocket.Connect(ipend, 2000);
                if (TimeOutSocket.IsConnectionSuccessful)
                {
                    isOpen = true;
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(isOpen);
        }