java 动态选择端口号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2231467/
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
Dynamically choosing port number?
提问by python dude
In Java, I need to grab a port number for communication between multiple instances of the same program. Now, I could simply pick some fixed number and go with it. But I'm wondering if there's a way to dynamically choose the port number, so that I don't have to bother my users with setting the port number.
在 Java 中,我需要获取一个端口号,以便在同一程序的多个实例之间进行通信。现在,我可以简单地选择一些固定的数字并使用它。但是我想知道是否有一种方法可以动态选择端口号,这样我就不必在设置端口号时打扰我的用户。
Here's one idea I had, which works like this:
这是我的一个想法,它的工作原理是这样的:
- There's a fixed initial port number A.
- Program 'MyApp' starts, tries to grab port A.
- If it succeeds, then it's the first instance of 'MyApp'. Done.
- If it fails, it asks over port A whether the program on A is an instance of 'MyApp'. If yes, communicate with that instance. Done. If not, try to grab port A+1. And if there's another program using that port (not an instance of 'MyApp' either), then grab A+2, then A+3, and so on.
- 有一个固定的初始端口号 A。
- 程序“MyApp”启动,尝试获取端口 A。
- 如果成功,则它是“MyApp”的第一个实例。完毕。
- 如果失败,它会通过端口 A 询问 A 上的程序是否是“MyApp”的实例。如果是,请与该实例通信。完毕。如果没有,请尝试抢占端口 A+1。如果有另一个程序使用该端口(也不是“MyApp”的实例),则获取 A+2,然后是 A+3,依此类推。
Does this strategy make sense? Or is there a better way to dynamically choose a port number?
这个策略有意义吗?或者有没有更好的方法来动态选择端口号?
回答by Chris Jester-Young
If you bind to port 0, Java will use a system-generated port. :-) So, that's probably the easiest way to fall back if your desired port is already used.
如果绑定到端口 0,Java 将使用系统生成的端口。:-) 所以,如果您想要的端口已经被使用,这可能是最简单的回退方法。
ServerSocket s = new ServerSocket(0);
int port = s.getLocalPort(); // returns the port the system selected
回答by Keith Adler
I would take the inverse and select a fixed high port for your app. Make it a config value so it could be changed if necessary. This will simplify configuration as often times users of apps need to request network operations to open ports. Work around the IANA assigned values:
我会反过来为您的应用程序选择一个固定的高端口。将其设为配置值,以便在必要时进行更改。这将简化配置,因为应用程序用户经常需要请求网络操作以打开端口。解决 IANA 分配的值:
http://www.iana.org/assignments/port-numbers
http://www.iana.org/assignments/port-numbers
Scanning ports could turn your app into a bad citizen for many intrusion detection systems.
扫描端口可能会将您的应用程序变成许多入侵检测系统的坏人。
回答by jldupont
You could use Bonjour/ZeroConfto advertise the services of each instance and enable an instance to find the others. Think of this as a directory servicewhich could help manage your port namespace.
您可以使用 Bonjour/ ZeroConf来通告每个实例的服务,并使一个实例能够找到其他实例。将此视为可以帮助管理端口命名空间的目录服务。
Each instance can just grab a dynamically assigned port in this case. A request for binding to port "0" will usually instruct the system to assign a dynamic port.
在这种情况下,每个实例只能获取一个动态分配的端口。绑定到端口“0”的请求通常会指示系统分配一个动态端口。

