Serv

The next episode in my “Dicking around with Ruby” series: Serv.

Serv is a very basic TCP server with a twist: it handles the footwork of waiting for lines and handling disconnections and multiple clients, and you provide the hooks for “when stuff happens”. It won’t do binary protocols, it won’t do protocols that don’t use line breaks everywhere and it won’t do UDP. However, I do believe it could be used to implement, say, an IRC server or an HTTP server.

For example:

require 'socket'
require 'serv'

serv = Serv.new(4242)

serv.add_hook(:introduce, proc {|par| 
    sock = par[:sock]
    sock.puts "Hey there! It's #{Time.now.utc.strftime('%H:%M:%S')}" +
        " UTC. Do you know where your children are?"
    serv.hdo :revoke, sock
})

serv.run

The socket guts of Serv is based on a short example put forward in Sockets programming in Ruby, although there’s not a lot that could be done differently, as evidenced by a quick search for similar examples. For this reason I release Serv into the public domain. No license.

Here’s that download link again.

Thoughts?

No comments yet.

Leave a comment

Your e-mail address is never shown. If you type a line break in the comment, it will show up as a line break (naturally). The following HTML is allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(required)

(required)


Please note: Your comment will not show up at once. Unless you're spamming or being abusive, you have nothing to worry about. (Read the full policy.)