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.





Thanks, I was thinking about a file to but was hoping to get some neater ideas here haha
– ed22
Jun 29 at 14:51






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.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift