How to make a Serverless java LAN Chat application with a user list
Joined: Jun 25, 2011
I made a very simple, easy to use, chat application for LAN that can communicate without a server and at the same time, provide the users with a user list so that they can see who's connected and who is not. The reason why I do not want to have a central server is because my home network does not have a computer that can act as a server all the time. So, the only way I can think of to implement this program is to make the application both a server and a client. The program runs a thread for a ServerSocket to wait for a connection, and uses a MulticastSocket and joins a multicast group to broadcast its presence on the network. When the same program on other machines receive the message, they will make a Socket connection to the one that broadcasted its presence. Here's a simple illustration:
1. LAN Chat "A" is running on a machine while LAN Chat "B" is on a different machine. Both of them are listening on the same multicast address and port and runs a server socket.
2. "B" sends a broadcast packet containing the port number of its ServerSocket.
3. "A" receives the message and connects to "B"s ServerSocket on the port number specified in the packet.
4. "A" and "B" are now connected. Now another LAN Chat, "C", starts on another machine, and broadcasts it's ServerSocket port to the multicast group.
5. "A" and "B" receive the packet and connect to "C"s server socket on the port specified in the packet.
6. "A", "B", and "C" are now connected.
The problem here is that datagram packets sent over a MulticastSocket does not have a guaranteed delivery. Let's say another LAN Chat, "D", starts on another machine and broadcasts its ServerSocket port to the multicast group. "A" and "B" received the packet but "C" didn't. So "A" and "B" connects to "D". "D" will never know that "C" exists because "C" did not receive "D"s packet.
To solve the problem, I made the application tell everyone in its list that a new user exists everytime it makes a successful connection to the new user. So here's what happens...
1. LAN Chats "A", "B", and "C" are all connected to each other when "D" starts on another machine.
2. "D" broadcasts its ServerSocket port number to the multicast group.
3. "A" receives the broadcast message and connects to "D" on the port number specified in the message. "A" then tells "B" and "C" that LAN Chat "D" exists. "A" gives "B" and "C" the IP address and port number of "D", so that they can connect to "D".
Now I solved the problem of the case of an undelivered datagram packet. "D" will now be able to know the presence of "C". The only problem now is that there will be unimportant messages that will be sent over the network, because every time a successful connection is made, the LAN Chat tells the other LAN Chats that a new LAN Chat exists.
However, the program works fine. But I'd like to know if there's a more proper way of implementing a serverless LAN Chat with user list. I want to get rid of the unecessary messages sent over the LAN.