How do you specify a port range for Java sockets?

后端 未结 4 1377
死守一世寂寞
死守一世寂寞 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:18

    You might glance at the java code that implements the function you are using. Most of the java libraries are written in Java, so you might just see what you need in there.

    Assuming @Kenster was right and it's a system operation, you may have to simply iterate over ports trying to bind to each one or test it. Although it's a little painful, it shouldn't be more than a few lines of code.

提交回复
热议问题