确定端口是否在使用中? [英] Determine if port is in use?

查看:17
本文介绍了确定端口是否在使用中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 C# 来确定端口是否可用?我想在使用已使用的端口启动 WCF ServiceHost 实例之前进行检查:-)

Is there a way, using C#, to determine if a port is available? I'd like to check before I start up a WCF ServiceHost instance using a port that's already used :-)

推荐答案

您无法确定端口是否可用.你只能确定

You cannot determine if a port is available. You can only determine

  1. 您可以控制端口
  2. 某个端口在过去的某个时间可用

除非您通过绑定特定套接字并侦听该端口来控制该端口,否则另一个进程可能会出现并控制该端口.

Unless you control the port by having a particular socket bound and listening on the port, it's possible for another process to come along and take control of the port.

了解端口是否可用的唯一可靠方法是尝试侦听它.如果您成功,则该端口可用并且您拥有控制权.否则,您会知道在过去和可能现在的某个时刻,该端口由另一个实体控制.

The only reliable way to know if a port is available is to attempt to listen on it. If you succeed then the port is available and you have control. Otherwise you know that at some point in the past and potentially the present, the port was controlled by another entity.

这篇关于确定端口是否在使用中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆