Oops. Forgot to finish this thought:
...The main problem is that it only displays messages sent from the
server when the user submits a message. So, a whole bunch of messages
may be "backed up", and even when the user submits a message, he only
sees the next message in the queue rather than all the messages that
were sent since his last submission. Obviously not how a chat app
should work.
Somehow, I have to get the client to behave how the server behaves,
i.e., to listen for any messages sent to it and respond to them
immediately. Still looking at the thread approach...
Hi Yaz!
Yes, chat app is a good example for threads...
Create a thread for sending messages to the server, and an other for
receiving messages, and create a "main" class, that will spawn these 2
threads at the program startup!
To communicate with the writer-thread, it must implement an interface!
If you want to process the message, received by the reader-thread, in
your main-class, it should implement an other interface as well!
And i think, that's all.
Any questions?
> Oops. Forgot to finish this thought:
> ...The main problem is that it only displays messages sent from the
[quoted text clipped - 7 lines]
> i.e., to listen for any messages sent to it and respond to them
> immediately. Still looking at the thread approach...
Yaz - 15 Mar 2005 16:02 GMT
Hi Daniel,
Thanks for the feedback. This thing has kept me up all night! I've got
the two threads going; one for sending, one for receiving. What's
tripping me up is how to implement the receving thread on the client.
I've tried a bunch of different ways and gotten more errors than I can
count. Can you explain specifically which interfaces need to be
implemented for which threads? Also, if I create the receiving thread
in main, should I create another class that either extends Thread or
implements Runnable, and should that class have a while loop that
handles the response from the server? That's what I'm trying to do, but
to no avail.
Thanks again!
Sameer - 15 Mar 2005 20:43 GMT
Why dont you read the IBM tutorial from Greg Travis about writing a
multithreaded Chat Server?
This IBM tutorial can be registered from IBM DevelperWorks and can be
downloaded also.
It uses blocking approach.
If you are interested in non-blocking approach i.e. using
ServerSocketChannel and SocketChannel then do download the source code
for the book Advanced NIO again from Greg Travis, Manning Publication.
Code for Polling Chat Server is given their.
Hope this will definately help you.
-Sameer75@g