groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Keith Suderman <suder...@anc.org>
Subject Re: Help with socket programming and loops
Date Mon, 25 May 2015 19:45:41 GMT
The code won’t break out of the loop because the readLine() method will block until there
is more input available.  Since you are reading from a socket the reader class doesn’t know
if more input is going to appear on the socket or not.

One solution is to have the client send a magic string that the server watches for.  When
the server sees the magic string arrive it knows it has reached the end of input and should
stop trying to read lines.

Another (better) solution is to have the client call socket.shutdownOutput() after it has
finished sending data. This lets the reader on the server determine when the end of the input
has been reached.

E.g.

	// In the client code.
	PrintWriter writer = output.newPrintWriter()
	while (weWantToSendData) {
		writer.println “Hello world”
	}
	writer.flush()
	// Signal that we are done with the output stream. We can still read but can’t write anymore.
	socket.shutdownOutput()
	// Print the server response
	println input.newReader().readLine()


	// In the server code
	BufferedReader reader = input.newReader()
	String line
	int count
	while ( (line = reader.readLine()) != null) {
		println “Read a line: $line”
		++count
	}
	// Send an ACK to the client.
	output.newPrintWriter().println(“Received $count lines”)


Hope this helps.

Cheers,
Keith

On May 25, 2015, at 11:48 AM, Eric MacAdie <emacadie@gmail.com> wrote:

> Hello Groovy users.
> 
> 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 MacAdie
> 

------------------------------
Research Associate
Department of Computer Science
Vassar College
Poughkeepsie, NY



Mime
View raw message