views:

78

answers:

1

I recently created a tcp server in groovy and am wondering how to get it to behave similar to telnet.

For example, I run my tcp server and pull up the cmd prompt to telnet the port of the script and send it commands that its looking for. Most of the commands send back one line/word of information. However there are a few that send back a large string (similar to a paragraph of information). It works fine with telnet.

However, when I create my tcp client, I can't get it to accept anything more than the first line of information.

I am using readLine() instead of readLines() because if I use readLines() it hangs there and doesn't allow me to send the next command.

I also tried something like: (psuedocode)

while((r.readLine()) !=null) {
    def a = r.readLine()
}

Which also just hangs there like readLines()

If you need to see what the code looks like, check here: http://stackoverflow.com/questions/3481579/groovy-tcp-client-server-sending-maps

My sendMessage function is different on those, but I changed it to be the same and am using the one I have on the client.


EDIT

In response to the first answer provided...

Well doing what you mentioned as well as something like:

while((line = r.readLine()) !=null) {
    println line
}

Both work, but the way I am sending commands is somehow interfering with the functionality of it.

If you take a peak at my source (on the link provided above) perhaps you can spot why it isn't working properly with those solutions.

A: 

Your pseudocode is wrong. On each loop iteration, you are reading a line to check when to end the loop, then reading another line into the variable a. This might be the problem. If your variable r is a Reader, try something like this:

r.eachLine { line ->
    def a = line
}
ataylor
I replied to your answer where it says **Edit** in my main post. I put it in there so it would accept the code tag
StartingGroovy