views:

77

answers:

3

Hello,

During my years at the university I had a personal project of making 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.

+1  A: 

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/

Thorbjørn Ravn Andersen
Thank you, but I don't think this fits my need. Seems like you have to configure some DNS records on a server? Like I said, by server I mean that my program acts like a server. I would like to run the program as server on any machine without any configuration (configuration that can be distributed along with the program is acceptable though). I haven't had the time to really read about JmDNS, so I might be wrong.
Stian
No. It works out of the box without any external servers or configuration, and I believe it fits your needs perfectly. (The DNS in the name refers to the ZeroConf technology is _based_ on multicast DNS but it does not require a DNS server).
Thorbjørn Ravn Andersen
Alright. But it doesn't seem to have any documentation whatsoever, so I'm gonna go with JGroups.
Stian
+2  A: 

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.

Peter Tillemans
Thank you! JGroups seems very interesting. I definately think I can use this, it even has a tutorial on making a chat program :P
Stian
+1  A: 

You could try to use multicast if it's enabled on your network.

Bruno