Java Network I/O

D

DaveL

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.
 
G

Gordon Beaton

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top