This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Chat server and threads" Watch "Chat server and threads" New topic
Author

Chat server and threads

Anand, Parulkar
Greenhorn

Joined: May 16, 2000
Posts: 2
hi
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:
controller.broadCast(message);
And broadcast should lock on some object and then send each thread the message.
broadCast() {
...
synchonize (lock_o) {
for (Enumeration e.... }
(ServerThread)e.sendMessage(message)
}
}
...
}
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 )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chat server and threads
 
Similar Threads
how do I know when client closes the browser ?
JMS push model asynchronous messaging
Serialization Prob in RMI
How to break blocked write operation???
Chat server with servlet.....