Java: How to make clients detect server on network?
During my years at the university I had a personal project of ma开发者_StackOverflowking a simple chat program in Java, using TCP sockets. The project was only for educational purposes.
One thing I wasn't very happy with was that a client had to know the IP of the server in order to connect to it. So I got to thinking it would be nice if the clients could be able to detect if there was any chat servers available on the network.
How can I accomplish such a mechanism? If you could point me in the right direction I'd be grateful. :-)
I'm thinking the server would have to do some kind of broadcast, but I'm not sure how to do this.
PS: I know there probably are other/better ways of doing this, but for now I'm specifically interested in how to make a client detect servers on the network.
And by server I mean the server-side part of the chat application, not like a Tomcat server.
There are many ways to do this, but I recommend you look at JGroups. It might be somewhat overkill for chat application but its use of multicast offers interesting possibilities like server-less architectures.
Look at ZeroConf which provide mechanisms for announcing and discovering services. Apple use this extensively under the name Bonjour.
The jmdns project is a Java implementation, which work quite well. http://jmdns.sourceforge.net/
You could try to use multicast if it's enabled on your network.
精彩评论