C#快速判断网络端口连通状态

 

using System.Net.Sockets;
using System.Net; 

  //完成TELNET
        private static string cmdTelnet(string strIP,int strNum)
        {
            string strTelnet;
           
            try
            {
                IPAddress ip = IPAddress.Parse(strIP);
                IPEndPoint point = new IPEndPoint(ip, strNum);
                Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                sock.Connect(point);
                strTelnet = string.Format("连接端口{0} 成功!",point);
            }
            catch(SocketException e)
            {
                if (e.ErrorCode != 10061)
                {
                    MessageBox.Show(e.Message.ToString(), "提示", MessageBoxButtons.OK);
                }
                strTelnet = string.Format("连接{0} 失败", strIP + ":" + strNum);
            }
            return strTelnet;
        }

你可能感兴趣的:(C#技术,c#,网络,string,socket)