I’m coding a client server pair in java and I would like the ports to be assigned at runtime by the system. From the server’s side this can be easily done through APIs but how does the client know which port the server is listening on?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generally speaking, you’ll need to use some other directory or discovery service to let the client know what port the server is on. There is no widely available, general purpose mechanism to ask a server “what port is server process X listening on”
If you’re just looking to have flexibility in the port assignment (for example, to be able to use different ports in different installations, or to have multiple instances of your server running on different ports), consider using a simple environment variable or command line override to feed a port to the server. E.g.,
Of course, you’ll still need to make the client configurable to find the server, but at least you’ll have predictability (and won’t be relying on dynamically system-assigned ports).