Home > Sysadmin > Writing a daemon in groovy

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.

Categories: Sysadmin Tags:
  1. Morsello
    June 10th, 2009 at 20:23 | #1

    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”

    }
    }
    }

  2. June 11th, 2009 at 15:39 | #2

    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 😉

  3. JamesD
    June 11th, 2009 at 17:08 | #3

    Thanks for the useful info. It’s so interesting

  1. June 10th, 2009 at 09:43 | #1
  2. June 10th, 2009 at 12:09 | #2