How do you specify a port range for Java sockets?

后端 未结 4 1381
死守一世寂寞
死守一世寂寞 2021-01-17 15:55

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

4条回答
  •  一向
    一向 (楼主)
    2021-01-17 16:38

    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 :/

提交回复
热议问题