windows 如何使用端口 1025-5000 作为临时端口来绕过 WinXP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2630009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I work around WinXP using ports 1025-5000 as ephemeral?
提问by Chris Dolan
If you create a TCP client socket with port 0 instead of a non-zero port, then the operating system chooses any free ephemeral port for you. Most OSes choose ephemeral ports from the IANA dynamic port rangeof 49152-65535. However in Windows Server 2003 and earlier (including XP) Microsoft used ports 1025-5000 as the ephemeral range, according to their bind() documentation.
如果您使用端口 0 而不是非零端口创建 TCP 客户端套接字,则操作系统会为您选择任何空闲的临时端口。大多数操作系统从IANA 动态端口范围49152-65535 中选择临时端口。但是,根据其bind() 文档,在 Windows Server 2003 及更早版本(包括 XP)中,Microsoft 使用端口 1025-5000 作为临时范围。
I run multiple Java services on the same hardware. On rare occasions, this range collides with well-known ports that I use for other services (e.g. port 4160 for Jini discovery). While rare, this has caused real problems. Is there any easy way to tell Windows or Java to use a different port range for client sockets? Microsoft's docs indicate that I can change the high end of that range via the MaxUserPort TcpIP registry setting, but I see no way to change the low end.
我在同一硬件上运行多个 Java 服务。在极少数情况下,此范围会与我用于其他服务的知名端口(例如,用于 Jini 发现的端口 4160)发生冲突。虽然很少见,但这已经引起了真正的问题。有没有什么简单的方法可以告诉 Windows 或 Java 为客户端套接字使用不同的端口范围?Microsoft 的文档表明我可以通过 MaxUserPort TcpIP 注册表设置更改该范围的高端,但我认为无法更改低端。
Update:I've made some progress on this. It looks like Microsoft has a concept of reserved ports that are exceptions to the ephemeral port range. There's a registry settingthat lets you change this permanently and apparently there must be an API to do the same thing because there's a data structurethat holds high/low values for reserved port ranges, but I can't find the actual function call anywhere... The registry solution may work, but now I'm fixated on this API.
更新:我在这方面取得了一些进展。看起来微软有一个保留端口的概念,它是临时端口范围的例外。有一个注册表设置可以让您永久更改它,显然必须有一个 API 来做同样的事情,因为有一个数据结构可以保存保留端口范围的高/低值,但我在任何地方都找不到实际的函数调用。 .. 注册表解决方案可能有效,但现在我专注于这个 API。
Update2:I accepted a solution on ServerFaultfor how to do this via the Windows registry. I'd still like a way to do it via API, but I guess I'm satisfied for now.
更新 2:我在ServerFault上接受了一个关于如何通过 Windows 注册表执行此操作的解决方案。我仍然想要一种通过 API 来完成它的方法,但我想我现在很满意。
采纳答案by torak
It's not as elegant as using OS support for ephemeral ports, but the docsshow that you should be able to specify a port for your socket to bind to. Specify a port at the base of the range you want and if it is used an exception will be thrown, in which case increment the port and try again. Given that windows isn't using the port range that you want, there shouldn't be many collisions.
它不像对临时端口使用操作系统支持那么优雅,但文档表明您应该能够为您的套接字指定一个端口来绑定到。在您想要的范围的基础上指定一个端口,如果使用它,将抛出异常,在这种情况下,增加端口并重试。鉴于 windows 没有使用您想要的端口范围,不应该有很多冲突。