How do you specify a port range for Java sockets?

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

    Binding the socket to any free port is (usually) a feature of the operating system's socket support; it's not specific to java. Solaris, for example, supports adjusting the ephemeral port range through the ndd command. But only root can adjust the range, and it affects the entire system, not just your program.

    If the regular ephemeral binding behavior doesn't suit your needs, you'll probably have to write your own using Socket.bind().

提交回复
热议问题