This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Chat server and threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Chat server and threads" Watch "Chat server and threads" New topic

Chat server and threads

Anand, Parulkar

Joined: May 16, 2000
Posts: 2
I am developing a chat server.(Initially without any GUI , just on DOS promt)
So i am able to connect as many no. of clients as i want (number not fixed)to server.In server i am starting a new thread for each client connection.And that thread will then take the input from that particular client and give the response back.
But each client i.e. each thread is seperate.So how to pass message coming from one client to all other clients that are connected to my server.
Plus i want that even if any client is not typing any text then also all other messages should be seen on that client.
But if on any client i am using "readLine()" method then the program waits until something is typed on that screen.
So how can i achieve this ?
Please tell me how to send messages across all clients.
[This message has been edited by Anand, Parulkar (edited May 17, 2000).]
[This message has been edited by Anand, Parulkar (edited May 17, 2000).]

anand parulkar
Pawel Veselov
Ranch Hand

Joined: Jan 14, 1999
Posts: 165
For the server you have as many threads as you want and a sort of controller, that has a Vector of all threads (better special interface) that can receive a message. So, when a special server thread wants to send a message to all of the other threads it asks this controller to do it:
And broadcast should lock on some object and then send each thread the message.
broadCast() {
synchonize (lock_o) {
for (Enumeration e.... }
Should look like that.
From the client side you can have two threads, one is for reading and second for getting the data over the network. So, when the client gets some data it puts it on the screen, and when readLine() finishes, you can send your data over the network.
The screen can go crazy, but JDK never had good console io.

With best of best regards, Pawel S. Veselov ( aka Black Angel )

With best of best regards, Pawel S. Veselov ( aka Black Angel )
jQuery in Action, 3rd edition
subject: Chat server and threads
It's not a secret anymore!