...
| Code Block |
|---|
class AccumulatorProxy {
def accumulate(args) {
def result
def s = new Socket("localhost", 54321)
s.withStreamswithObjectStreams{ inputois, outputoos ->
def oos = new ObjectOutputStream(output)
def ois = new ObjectInputStream(input)
oos.writeObject(args)<< args
result = ois.readObject()
ois.close()
oos.close()
}
s.close()
return result
}
}
println new AccumulatorProxy().accumulate([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
// => 55
|
...
| Code Block |
|---|
class Accumulator {
def accumulate(args) {
args.inject(0){ total, arg -> total += arg }
}
}
def port = 54321
def accumulator = new Accumulator()
def server = new ServerSocket(port)
println "Starting server on port $port"
while(true) {
server.accept() { socket ->
socket.withStreamswithObjectStreams { inputois, outputoos ->
def ois = new ObjectInputStream(input)
def oos = new ObjectOutputStream(output)
def args = ois.readObject()
oos.writeObject( << accumulator.accumulate(args))
ois.close()
oos.close()
}
}
}
|
This example was inspired by this Ruby example.