Hi everyone,

I'm in a situation where I would like to have a ServerSocket that listens on a specific port. However, if this port is already taken, I would like to listen on a different port. How can I advertise what port my Java program is listening on, so that clients who are connecting to my machine know what port to connect to? Otherwise I do not see how a client would know where to connect to. If anyone has any useful links, advice, or even search terms that would be helpful, as I don't see anything. Thanks.

Sort of like your telephone number. If you change it, how will your friends know the new number.
Have a web site/page where you post the number that can be read by the software that needs to know your current port.

What other software is taking the number?

Sort of like your telephone number. If you change it, how will your friends know the new number.
Have a web site/page where you post the number that can be read by the software that needs to know your current port.

What other software is taking the number?

I have a Server that sits on a port and offers a Service. Any client that wants to connect should be able to. The problem is, how does the Client know what port my Server is listening on? The port could be hardcoded, but then if another program is already using the port, it will fail. PS, I'm fairly certain there is a better way than advertising it on the web. Maybe I'm going about this wrong though.

Check out multicast if you really feel the need to advertise. Note that this will probably only be a "local network" feature as most gateways will probably not propagate the signal.

Better, however, would be to simply have a range of, say 5 or so, ports and simply have the clients try one after the other until they get a connection (or not if the server is down or there is a network problem).

Edited 6 Years Ago by masijade: n/a

The problem with testing multiple ports is that the OP said there is some other software using the normal port causing the problem. So the client would connect to this other software that is using the port. Things would be ok if the client recognized that the software it was talking to was not the desired target.

That's why he should be using some sort of "start" protocol in the first place. Either the client or the server blindly expecting a successful socket connection to mean that they have the "right" connection is bad design in the first place.

That's why he should be using some sort of "start" protocol in the first place. Either the client or the server blindly expecting a successful socket connection to mean that they have the "right" connection is bad design in the first place.

Agreed, it would be, and yes, there is a start protocol (although it was not specifically designed with the intent to thwart "bad" connections). I think I will just try various ports in order then. I did know you could do that, but I figured a better practice would be some way to advertise what port you're on. Thanks for the replies guys.

Since you would assume that at least 90% of the time you will be able to reserve your designated port, the "loop" will only come into play, at most, 10% of the time. Whereas if your "advertising" the port, the client would have to make two connections every time.

This question has already been answered. Start a new discussion instead.