如何为Java套接字指定端口范围? [英] How do you specify a port range for Java sockets?

查看:124
本文介绍了如何为Java套接字指定端口范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,您可以将数字0作为Socket或DatagramSocket构造函数的单个参数。然后,Java将Socket绑定到一个空闲端口。是否可以将端口查找限制在特定范围内?

In Java you can give the number zero as a single parameter for the Socket or DatagramSocket constructor. Java binds that Socket to a free port then. Is it possible to limit the port lookup to a specific range?

推荐答案

Hrm,在阅读完文档之后,我不认为您可以。您可以绑定到任何端口,然后重新绑定(如果不可接受),或重复绑定到您的范围中的端口,直到成功为止。第二种方法将是最有效的。

Hrm, after reading the docs, I don't think you can. You can either bind to any port, then rebind if it is not acceptable, or repeatedly bind to a port in your range until you succeed. The second method is going to be most "efficient".

我对这个答案感到不安,因为它不太优雅,但我真的找不到任何东西否则:/

I am uneasy about this answer, because it is... inelegant, yet I really can't find anything else either :/

这篇关于如何为Java套接字指定端口范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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