The sample works by sending a line of data as a request; thus the server calls readLine() and the client sends a new line character at the end of the input. The server echos the request back to the client along with a timestamp. For those that skip reading the documentation, ServerSocket's accept method "Accepts a connection and passes the resulting Socket to the closure which runs in a new Thread." so this server creates a new thread for each request.
Groovy Socket server
import java.net.ServerSocket
def server = new ServerSocket(4444)
while(true) {
server.accept { socket ->
println "processing new connection..."
socket.withStreams { input, output ->
def reader = input.newReader()
def buffer = reader.readLine()
println "server received: $buffer"
now = new Date()
output << "echo-response($now): " + buffer + "\n"
}
println "processing/thread complete."
}
}
Groovy Socket client
s = new Socket("localhost", 4444);
s.withStreams { input, output ->
output << "echo testing ...\n"
buffer = input.newReader().readLine()
println "response = $buffer"
}
Give it try! Hope this helps...