0

I am trying to create an http server using JDK's com.sun.net.httpserver. I am able to run the server with certain endpoints successfully. And it return some random ephemeral port ranging from 0 to 65536.

HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);

I was going through the documentation but I couldn't see a way to specify some range instead of specific port. Is there any way passing range can be achieved?

One hack to the solution is try for all ports in range and see whichever works as specified in the solution - How to find an available port?

Rahul
  • 326
  • 2
  • 10
  • You cannot pass a range. If you don't specify a port, the operating system will pick one for you. The only way of making sure that a range can be used is to use that workaround. – f1sh Jan 11 '23 at 12:48

0 Answers0