简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM