Skip to content
Advertisement

Java: Start a socket server class with the port as an argument

So i have a load balancer and when a server is full of clients i want to create a new multithread server programmatically by passing the server port as an argument.

This is how im trying to start a new server instance

JavaScript

this is my server

JavaScript

my question is how to start this server with the giver port argument? is this code correct?

Advertisement

Answer

You are passing 0 to the ServerSocket constructor, so it will choose an available port. You need to pass a non zero port number if you want to use a specific port.

You could do it like this:

JavaScript

Notice that I’m assigning the port parameter to the port field, and then passing it to the ServerSocket constructor.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement