0

I am trying to do some socket programming in Groovy. I am using code found here as a template:

http://programmingitch.blogspot.com/2010/04/groovy-sockets-example.html

My problem is with the server code.

It reads in the input buffer from a reader using the reader.readLine() method. It turns out it's a BufferedReader.

A problem that I am having is: What if the input is more than one line?

I have been trying for a few hours to get this.

Neither reader.getText(), reader.readLines() nor reader.eachLine() worked.

I tried a few things like this:

def reader = input.newReader()
def cc = reader.eachLine {
    println "Here is the next element: ${it}"
}
println "Done iterating"

and

def reader = input.newReader()
def iter = reader.iterator()
while ( iter.hasNext() ) {
    println "Here is the next element: ${iter.next()}"
    println "About to get next, do we have next? "
    println "iter is a ${iter?.getClass()?.getName()}"
}
println "Done iterating"

and

def holdLine
while ( ( holdLine = reader.readLine() ) ) {
    println "holdLine: ${holdLine}"

}
println "Done"

In each case, it reads each line, but it does not get to the line after the loop.

Maybe there is something obvious that I am missing and I am making a mistake only a noob would make, but why won't the code break out of the loop?

Eric
  • 31
  • 4

1 Answers1

0

BufferedReader is blocking, which means that it waits for input until it becomes available. The following post suggest you get hold of the InputStream object the BufferedReader wraps.

How can I read from a BufferedReader in Java without blocking?

HTH

Community
  • 1
  • 1
Gerard van Helden
  • 1,601
  • 10
  • 13