How can client application find dynamically selected TCP port opened by server (Java)?
How can client application find dynamically selected TCP port opened by server (Java)?
I have a windows service which opens a TCP port (one that was available at given moment). Now I need to connect a client to that port. How do I let the client know which port the server is running on?
2 Answers
2
Unless you have some way to communicate with the service from your Java program, this will not be that simple. One solution could be to try parsing the output of the netstat
command. For more details on how to do that you can look at this question
netstat
You can't really solve this in the general case without either having the client try all the ports until it somehow recognizes the server, or communicating by some other method.
The easiest solution is to agree on a file name in an agreed location (the temp directory, for example) and store the port number in that file.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks, I was thinking about a file to but was hoping to get some neater ideas here haha
– ed22
Jun 29 at 14:51