Question about sockets

Discussion in 'Java' started by stathis gotsis, Feb 6, 2006.

  1. 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
    stathis gotsis, Feb 6, 2006
    #1
    1. Advertising

  2. stathis gotsis

    Chris Smith Guest

    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
    Chris Smith, Feb 7, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Atirya Yodha

    Question with regard to Sockets

    Atirya Yodha, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    440
    Thomas Jollans
    Mar 7, 2004
  2. Jerem38

    a simple question about sockets

    Jerem38, May 5, 2004, in forum: Java
    Replies:
    4
    Views:
    334
    Michiel Konstapel
    May 7, 2004
  3. Stefan Willmert

    Applets, Security, Sockets General Question

    Stefan Willmert, Jun 5, 2004, in forum: Java
    Replies:
    3
    Views:
    518
    Stefan Willmert
    Jun 5, 2004
  4. Ike

    Sockets Question

    Ike, Sep 10, 2004, in forum: Java
    Replies:
    3
    Views:
    461
    Chris Smith
    Sep 10, 2004
  5. pythonhda

    URL emulator (sockets question)

    pythonhda, Aug 14, 2003, in forum: Python
    Replies:
    0
    Views:
    348
    pythonhda
    Aug 14, 2003
Loading...

Share This Page