c# 端口

这几天  老总让写一个检测本地端口是否启用的程序,网上找了好多的例子  都不能达到理想的效果,最后勉强能实现效果

在次  总结了一下  有关端口的各种问题(这里有用别人总结好的东西)

---能否拼通端口,即 端口是否已经打开  这个效果很明显 

 private static bool cmdTelnet(string strIP, int strNum)
        {

//参数介绍  第一个是IP  第二个是端口号
            bool 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);
                strTelnet = true;
            }
            catch (SocketException e)
            {
                if (e.ErrorCode != 10061)
                {
                    MessageBox.Show(e.Message.ToString(), "提示", MessageBoxButtons.OK);
                }
               // strTelnet = string.Format("连接{0} 失败", strIP + ":" + strNum);
                strTelnet = false;
            }
            return strTelnet;
        }

 

--检查端口是否被占用  --这个占用可以用
Process p = new Process();
p.StartInfo = new ProcessStartInfo("netstat", "-a");
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string result = p.StandardOutput.ReadToEnd().ToLower();
if (result.IndexOf(Environment.MachineName.ToLower() + ":4000") >= 0)
MessageBox.Show("4000端口被占用");
else
{
MessageBox.Show("ok");
}

-----------第三种不太理解,也没有达到预期的效果,拿出来大家研究一下吧

  public static bool GetPortMessage(string ip,int port)
        {
            bool tcpListen = false;
            bool udpListen = false;
            //设定端口状态标识位
            System.Net.IPAddress myIpAddress = IPAddress.Parse(ip);
            System.Net.IPEndPoint myIpEndPoint = new IPEndPoint(myIpAddress, port);
            try
            {
                TcpClient tcpClient=new TcpClient();
               
                tcpClient.Connect(myIpEndPoint);                            
                //对远程计算机的指定端口提出TCP连接请求
                tcpListen=true;
            }
            catch
            {             
            }
            try
            {
                UdpClient udpClient=new UdpClient();
                udpClient.Connect(myIpEndPoint);
                //对远程计算机的指定端口提出UDP连接请求
                udpListen=true;
            }
            catch
            {
             
            }
            if(tcpListen==false&&udpListen==false)
            {
                //MessageBox.Show("8000端口关闭!","提示");
                return false;
            }
            else
            {
                //MessageBox.Show("8000端口打开!","提示");
                return true;
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值