如何使用端口 1025-5000 作为临时端口来绕过 WinXP? [英] How can I work around WinXP using ports 1025-5000 as ephemeral?

查看:21
本文介绍了如何使用端口 1025-5000 作为临时端口来绕过 WinXP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您使用端口 0 而不是非零端口创建 TCP 客户端套接字,则操作系统会为您选择任何空闲的临时端口.大多数操作系统从 IANA 动态端口范围 49152-65535 中选择临时端口.但是,在 Windows Server 2003 和更早版本(包括 XP)中,Microsoft 根据他们的 bind() 文档.

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 range of 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.

我在同一硬件上运行多个 Java 服务.在极少数情况下,此范围会与我用于其他服务的知名端口发生冲突(例如,用于 Jini 发现的端口 4160).虽然很少见,但这已经引起了真正的问题.有没有什么简单的方法可以告诉 Windows 或 Java 为客户端套接字使用不同的端口范围?Microsoft 的文档表明我可以通过 MaxUserPort TcpIP 注册表设置更改该范围的高端,但我认为无法更改低端.

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.

更新:我在这方面取得了一些进展.看起来微软有一个保留端口的概念,它是临时端口范围的例外.有一个 注册表设置这让您可以永久更改它,显然必须有一个 API 来做同样的事情,因为有一个 数据结构 保存保留端口范围的高/低值,但我在任何地方都找不到实际的函数调用......注册表解决方案可能有效,但现在我'我专注于这个 API.

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 setting that lets you change this permanently and apparently there must be an API to do the same thing because there's a data structure that 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.

更新 2: 我接受了 ServerFault 了解如何通过 Windows 注册表执行此操作.我仍然想要一种通过 API 实现的方法,但我想我现在很满意.

Update2: I accepted a solution on ServerFault for 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.

推荐答案

它不像使用操作系统支持临时端口那么优雅,但是 docs 表明您应该能够为您的套接字指定要绑定的端口到.在您想要的范围的基础上指定一个端口,如果使用它,将抛出异常,在这种情况下,增加端口并重试.鉴于 windows 没有使用您想要的端口范围,应该不会有很多冲突.

It's not as elegant as using OS support for ephemeral ports, but the docs show 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.

这篇关于如何使用端口 1025-5000 作为临时端口来绕过 WinXP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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