在.NET中如何检测服务器上的TCP端口是否打开和/或活动?

发布于 2024-09-28 13:48:37 字数 121 浏览 1 评论 0原文

我需要与一台远程计算机建立 TCP 连接,但在此之前,我需要检测该端口是否正在被另一应用程序使用(如果该端口上已存在一个连接)。换句话说,我想确保一次只有一个应用程序使用同一端口。

在.NET 中如何做到这一点?

I need to establish a TCP connection with a remote machine, but before I do that, I need to detect, if the port is in use by another application (if there is already one connection on this port). In other words I want to ensure that only one application is using the same port a time.

What is the way to do it in .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2024-10-05 13:48:37

另一个应用程序是什么意思?客户端可以连接到远程服务器端口,如果没有任何内容侦听此端口,TcpClient 将抛出异常。因此尝试连接并捕获异常。如果侦听此端口的服务器应用程序与客户端使用的协议不同,当您尝试发送和读取数据时,将会抛出异常,因为数据不符合要求。这些都是特例。

What do you mean by another application? The client can connect to a remote server port and if there's nothing listening to this port TcpClient will throw an exception. So attempt to connect and catch the exception. If the server application listening on this port doesn't talk the same protocol as the client an exception will be thrown when you try to send and read the data as it won't conform. All these are exceptional cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文