File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chat server and threads

 
Anand, Parulkar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Pawel Veselov
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic