Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Java Network I/O

Thread view: 
DaveL - 17 Apr 2006 05:21 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.