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 / February 2006

Tip: Looking for answers? Try searching our database.

Question about sockets

Thread view: 
stathis gotsis - 06 Feb 2006 23:45 GMT
Hello,
  I have the following segment of code in a program:

in = new BufferedReader(new InputStreamReader(
                                       mailSocket.getInputStream()));

,in which a mailSocket is an object of Socket class.

Later on, i am using this expression:

while ((serverOutput = in.readLine()) != null)
     System.out.println(serverOutput);

/*other stuff here*/

,wanting to print all the lines available on my end of the Socket at the
time being. This readLine() method is blocking, thus preventing the program
from getting on to the other stuff. How can i avoid this?

  Thank you for any help
Chris Smith - 07 Feb 2006 00:58 GMT
> while ((serverOutput = in.readLine()) != null)
>       System.out.println(serverOutput);
[quoted text clipped - 4 lines]
> time being. This readLine() method is blocking, thus preventing the program
> from getting on to the other stuff. How can i avoid this?

If you don't want blocking I/O, you can't use readLine at all.  You need
to use a SocketChannel and a CharBuffer to do the I/O.  The code looks
considerably different, and will be tougher to understand... but in
conjunction with intelligent use of java.nio.Selector, it's potentially
the most efficient way to do things.

Another way out of the problems of blocking I/O is to continue to use
blocking I/O, but also use multiple threads.  You can spawn a new thread
to read from each possible client of the socket.  This is clearly not
scalable to very high numbers of concurrent connections, but it's a heck
of a lot easier than the NIO way.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



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



©2009 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.