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

  • Post author:
  • Post category:其他


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;

}



版权声明:本文为kissyougoodby原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。