Results 1 to 6 of 6

Thread: Blocking hasNextLine() method

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Blocking hasNextLine() method

    Hello,
    I use a scanner to read data from a socket. I do not know how many rows the server will send me back, I am thus:
    Code:
    while(monScanner.hasNextLine())
    {
         String tm = monScanner.nextLine();
         / / Processing
    }
    However, this loop blocks the hasNextLine () last name, which must return false. I do not know why. How to prevent it from blocking?

    Thank you

    Fred

  2. #2
    Join Date
    Nov 2009
    Posts
    347

    Re: Blocking hasNextLine() method

    Hello,
    If the remote machine has not closed the socket is completely normal. Scanner uses a buffer that reads data from the source, and finds himself blocked by the read ().
    So two solutions:
    - Either the remote machine will no longer use the socket and it should close.
    - Either it must still use it, and there you are obliged to observe a certain protocol to know the amount of data to read without being blocked.

  3. #3
    Join Date
    Dec 2009
    Posts
    204

    Re: Blocking hasNextLine() method

    Hey
    In fact, the problem is that I use this reader for a small project Server / FTP client. When I make a list, I do not know when I'll stop reading, but I can not afford to close the socket under the pretext that the client will block. What do you think when you talk about protocol? Not because I see which protocol I use to tell my client "you will read 10 lines". If you any solution for this then please guide me.

  4. #4
    Join Date
    Nov 2009
    Posts
    359

    Re: Blocking hasNextLine() method

    Hello,
    The communication protocol between your client and your server: you are obliged to have one to communicate. it's just the rules of communication (style, I send "this", I replied "that" if it's good or "something else" if it is not good). When you talk about FTP, you mean the real FTP (ie respecting RFC) Is an application or file transfer using its own commands? Because normally with FTP when you make a LIST server simply returns a response style OK or NOT OK and data is sent over another socket, which is closed at the end.

  5. #5
    Join Date
    Dec 2009
    Posts
    204

    Re: Blocking hasNextLine() method

    Hello,
    The goal is to make a "pseudo" customer / ftp server, it does not make a real server and ftp client, because the course we do not, it's too big to respect the RFC. So, I think I'll pass the number of files before sending the list actually. The only thing I did not understand why this function is "verification" that does that look if there is something to read can block?

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Blocking hasNextLine() method

    Hello,
    Scanner reads theInputStream and stored in a buffer so that the parser. So when you make a hasNextLine () it reads as a buffer or in theInputStream to see if there are data. But if the server finished sending data class scanner will block because the buffer is empty and the method read () 'InputStream' will block (because there's nothing to read). Indeed, read () not return -1 as in the case where the end of stream is reached, and a socket that only occurs when closing the socket. So if you want to keep the socket open you have to use another means to prevent blocking read and move on.

Similar Threads

  1. Method overriding versus method hiding in C#
    By ^MALARVIZHI^ in forum Software Development
    Replies: 4
    Last Post: 25-12-2010, 06:25 AM
  2. Is it possible to call destroy() method within init() Method?
    By Level8 in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:36 AM
  3. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM
  4. Java: How can I call one method in another method?
    By biohazard 76 in forum Software Development
    Replies: 3
    Last Post: 16-07-2009, 07:12 PM
  5. Blocking the Pop-Up
    By Steinbach in forum Technology & Internet
    Replies: 3
    Last Post: 26-02-2009, 09:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,140,220.23621 seconds with 16 queries