简体   繁体   English

如何找到未使用的端口?

[英]How can I find an unused port?

I need to create a brain-dead HTTP server that returns 404s for everything on localhost. 我需要创建一个脑死亡的HTTP服务器,为localhost上的所有内容返回404。 Specifically, I have a program under an acceptance test harness which calls out to a server, but for testing I want to save time by skipping that unrelated check. 具体来说,我有一个程序在验收测试工具下调用服务器,但为了测试我想通过跳过那个无关的检查来节省时间。

I have a way to pass in the URL the program uses as part of the test harness, and the test harness (currently) just creates such a server on port 80. 我有办法传入程序用作测试工具一部分的URL,测试工具(当前)只是在端口80上创建这样的服务器。

However, a problem appears if more than one test attempts to run at the same time, because each test harness tries to create an HTTP server on port 80, which fails for one of the harnesses. 但是,如果多个测试尝试同时运行,则会出现问题,因为每个测试工具都尝试在端口80上创建一个HTTP服务器,该服务器对其中一个线束失败。

I'd therefore like to randomize the port and ensure it is available before attempting to create the HTTP server. 因此,我想在尝试创建HTTP服务器之前随机化端口并确保它可用。 How can I check if that port is in use? 如何检查该端口是否正在使用?

Bind a socket to port 0, the system will pick an available port, between 1024 and 5000 I believe 将套接字绑定到端口0,系统将选择一个可用端口,我相信在1024和5000之间

You can later learn that assigned port with the Socket.LocalEndPoint property. 您可以稍后使用Socket.LocalEndPoint属性了解分配的端口。

I'd have thought the answer is obvious: if a socket listen fails, it's not available: choose another. 我认为答案是显而易见的:如果套接字监听失败,它就不可用:选择另一个。 Just listen to the exceptions. 听听例外情况。

Why not mock out the request altogether so that you don't actually need a server or a network connection at all? 为什么不完全嘲笑请求,以便您根本不需要服务器或网络连接?

That would make for a much less brittle test. 这将使一个不那么脆弱的测试。

Ok, in any case, could you not just generate a random port in some predefined range, and try to set up a server on it? 好的,在任何情况下,您是否可以在某个预定义范围内生成随机端口,并尝试在其上设置服务器? If it is already occupied, an exception should be thrown. 如果它已被占用,则应抛出异常。

Socket.Select()

IIRC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到NetTcpBinding(WCF)免费的TCP端口(因此服务器可以绑定到它) - How can I find a TCP port that is free with NetTcpBinding (WCF) (so a server can bind to it) 如何轻松找到未使用的公共方法/属性 - How to easyily find unused public methods/properties 如何查找大型.NET应用程序中未使用的部分? - How to find unused parts of a large .NET application? 如何找到.Net Remoting分配的端口号? - How do I find which port number .Net Remoting Allocates? 为什么在ConnectionRefused后获得未使用的本地主机/端口组合的AddressAlreadyInUse? - Why do I get `AddressAlreadyInUse` for an unused local host/port combo after a `ConnectionRefused`? 如何使用Delphi Prism进行串行端口通信? - How I can use Delphi Prism to Serial Port Communication? 如何获得WCF服务的侦听地址/端口? - How can I get the listening address/port of a WCF service? 如何通过组合框将属性设置为串行端口? - how can i set properties to serial port from a combobox? 如何组织代码与串口设备通信? - How can I organize code for talking to a serial port device? 如何改善这个表现不佳,可怕的串口代码? - How can I improve this underperforming, terrible Serial Port code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM