简体   繁体   中英

Find used server ports from client in java

So we have a server that has multiple applications using multiple ports. 5729, 7614, 9231 as examples.

A client needs to connect to one of these ports. All ports are active and waiting for a connection on the server, but the server application the client needs to connect to is only on port 7614. The other two ports are for different applications.

Once an active port is connected to, the client is able to ping the server at the port and find out what application its using (this is already implemented knowing the server's protocols and works fine).

The problem is, the client has no way of knowing what port the correct port is going to be until it finds it and pings it. The port the server application uses is not static and may change at any time (not our code so it can't be fixed to be made static).

Currently, since we have no idea what port is going to contain the correct port number, we have to increment through every port from 0 to 65525, attempt to connect, and ping the ports that do connect to determine if we connected to the correct application.

This takes forever. Is there any way to get a list of the 3 ports that are in use? That is, any port the server applications have called new ServerSocket(port number) on?

This has to be done in java.

Edit The client needs to be able to see what ports the server is currently using. everything in this question has to be done client-side. Using files or running commands on the server is not an option.

Options:

  1. Change your design and use fixed port numbers. Scanning is never going to work. You could connect to anything.
  2. Use a naming service such as LDAP, and have each service bind itself with its actual port number into LDAP under a name that the client knows; have the client lookup that name and use that port number. I don't recommend this.
  3. Write your own registry service, to accomplish much the same as (2) but without having to grapple with the LDAP API: just have each service send its name and port number to the registry, and have the client ask the regs urge for each port number by name. Again thisnisnwrtomg extra code just to overcome a poor design.

I strongly recommend (1). This is what everybody else does.

For client to connect to server, Client should always know server port. But as for your case, server keeps on changing the port. Then if you have control over server design, change this as its very bad design, else have service on server from which client can ask the current server port.

If all of this is beyond the scope, then you are left with worst option which you are already doing ie scanning on the port. Then I can only say that keep scanning of server port as separate service, which keeps on scanning at some heartbeat and saves the last port. Make this service multithreaded so that you can ping on multiple ports parallely. When client starts, it can try to connect on the last saved port, if that works fine..its good else ask your service to start scanning again. Again..this is the worst design. One thing you can also do in your client design is that decouple the client init phase from server connection phase. So that if you are waiting on port scanning, you can still do some useful work in your client.

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