Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Question about sockets

Reply
Thread Tools

Question about sockets

 
 
stathis gotsis
Guest
Posts: n/a
 
      02-06-2006
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


 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      02-07-2006
stathis gotsis <> wrote:
> 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?


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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sockets Question Ike Java 3 09-10-2004 05:26 PM
Applets, Security, Sockets General Question Stefan Willmert Java 3 06-05-2004 08:12 PM
a simple question about sockets Jerem38 Java 4 05-07-2004 08:23 PM
Question with regard to Sockets Atirya Yodha Java 1 03-07-2004 04:34 PM
URL emulator (sockets question) pythonhda Python 0 08-14-2003 03:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57