Hi, I'm a 2nd year undergrad with a personal project on my agenda.
The project I've been working on is a ChatApplication under the
Client/Server Architecture, which is still in its early stages. I will
do my best to explain my current problem.
As a user for most ChatApplications, such as MSN, ICQ,
YahooMessenger...
A conversation is usually started by either initiating a "Conversation
Window" from the "Contact List GUI", or if incoming messages from other
users are detected. Regardless of how a conversation is started, once
it has been started, a Thread that listens for the incoming messages
from your conversation partner is usually required for a responsive
GUI. Here's a very simple example of a Thread I wrote.
-------------------------------------------------------------------------------
1 private class IncomingListener extends Thread {
2 public IncomingListener() {
3 start();
4 }
5
6 public void run() {
7 String msg = theObjChat.readLine();
8 displayArea.append(msg);
9 }
10 }
-------------------------------------------------------------------------------
My problem is, since read methods (such as read( ) or readLine( ) )
BLOCKS until input is available, when I want to close the conversation
window between me and my partner, the Thread which should always listen
for incoming messages won't be cleaned up (terminated). Since the
Thread will keep hanging on line7 becuase readLine( ) blocks.
My knowledge in the java network i/o is very entry level, if any
solution or advice could be offered, it would be kindly appreaciated.
Thank you very much for taking the time to read through.
Regards,
Dave L.
Gordon Beaton - 17 Apr 2006 09:34 GMT
> My problem is, since read methods (such as read( ) or readLine( ) )
> BLOCKS until input is available, when I want to close the
> conversation window between me and my partner, the Thread which
> should always listen for incoming messages won't be cleaned up
> (terminated). Since the Thread will keep hanging on line7 becuase
> readLine( ) blocks.
You could close the socket, which should force readLine() to return
with an error.
You could use a java.nio.channels.Selector to determine when it's
"safe" to read from the Socket, instead of blocking in the call to
readLine().
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e