Writing a daemon in groovy
I actually need to write a little daemon based on the JVM (I’ll explain why in a future post). As the groovy fan I am, I was looking for a ready to use receipt, this one is interesting but show only how to write, not to read
After getting some help from Guillaume here a working sample:
import java.net.ServerSocket import net.asyd.nagios.Hello def listenPort = 4242 def server = new ServerSocket(listenPort) while(true) { server.accept { socket -> println "new connexion" socket.withStreams { input, output -> def reader = input.newReader() def buffer = reader.readLine() output << "Hello world " + buffer + "\n" } } }
As you can see it’s very simple, thanks to groovy, once again. A thread will be create for each client.
def server = new java.net.ServerSocket(4242)
while(true) {
server.accept { socket ->
println “new connexion”
socket.withStreams { input, output ->
output << “Hello world ” + input.newReader().readLine() + “\n”
}
}
}
I have to do the same thing to publish a crossdomain policy file for flex because i used Rest to the conexion .. sooo i need socket (no http) and i need to build a xmlserversocket (flex call this) to connect a grails rest app with my app in flex.
This is the post : http://blog.govisualperu.com/?p=196
Thanks for you sample
Thanks for the useful info. It’s so interesting