• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java ChatRoom Questions.

hitokiri

Limp Gawd
Joined
Jan 1, 2001
Messages
178
I'm trying to figure out how to create a Java based chat room. I have the following in mind but am stuck at the point where how do I broadcast to everyone in the chat room?

The following pseudo code:

Server sits in while loop listening for clients on a port
Clients connect to server, server creates a new thread for the client.
Within the new thread, there will be another 2 threads for incoming and outgoing msgs.

Now, this is the point where i'm not sure if it'll work or not.

Lets say a client sends a message in the chat room, now the server receives the message and must send the message back to everyone in the chat room. I'm not sure if this is possible or not, but I was thinking the server can keep a link list of all clients and just do a output to them all in the main server program in which created all the threads.

Would this be the right way of implementing a chat room and are there any simpler implementations?

Thanks

- hito
 
I am not quite sure if you are wanting to code this all yourself, but have you taken a look at Jabber. Its basically a kick ass protocol for writing communications software. Plenty of open source Java written servers out there too. Start at www.jabber.org. If you want to get up and running quickly, take a look at Wildfire. Jive also has a kick ass java api called Smack for creating chat clients that connect to the jabber server.

-Heaton
 
Is this just for simple learning? You could look into using RMI (remote method invokation).
 
each client needs its own thread then create a class as a thread handler to distribute messages to each client.

not perfect...but its how i made mine :)

edit: i just read ur psuedo code....ur on the right track....i dont know if each client needs 2 threads though.

Since everything in java is an object...including each client(thread) a thread handler would be a while loop that just continuously checks for updates in each thread and replicates them to the others.
 
Thank you for the replies all. Yes, this is basically for my own learning experience. My professor said that I had an option of doing it if i wished and he'll take either this or an exam score, but it's mainly for learning experience. I have the code all written out, just about 95% done. It will work perfectly if the client only exits in the correct fashion though.. :p

The way I have it now is:

Client class initiates the connection and then creates a new thread in which the thread will try to see if there is information available(*someone sent a msg*), if not it will just wait until it is notified by another thread in which is inserting a message. After the message is updated it will notifyall the threads and they will one by one read the message and update their corresponding displays. So the one client basically has itself and another listening thread, server creates a listening and talking thread per client, and to tie this all together there's a class which updates and sends the messages in synchronized methods.

The issue that i'm having now is how to detect a abrupt application termination. For instance if the client's system crashed or the sort, the server for some reason will still continue to think it's a good thread and it ruins my semaphore. Where when a thread inputs data into the class that holds the message, it will detect how many active threads there are and set a counter to this number. The clients will one at a time get data from the class until the counter hits 0 (everyone has been served).

Anyone have any ideas?

Thanks
- hito
 
The thread should detect the crashed client when it tries to send the msg because writing to the corresponding Socket object should throw an IOException. In your catch clause, call a synchronized method of your "tie-together" that does the cleanup you need (such as deleting threads for the client and updating the client list/count).
 
Back
Top