Socket programming in Ruby
I was recently asked if I had the content of some articles that I posted a long time ago on a blog I used to run. After some searching I managed to scrape together the content using the way-back machine. It’s faithfully recreated here without changes, something I should have done when I first bought the barkingiguana.com domain.
I decided today to find out how hard it would be to do socket programming in Rails - mainly because I did a huge chunk of work and could find nothing better to do (the alternative was to tidy the kitchen…)
A little Google brought up an extract from the Pragmatic Ruby book at http://www.rubycentral.com/book/lib_network.html and this simple library reference has proved very handy. I’ll need to purchase a copy of that book. Somebody remind me when I’m feeling flush.
Anyway, unsurprisingly, it’s very easy to do socket programming - this is how you open and work with a TCP socket.
First we fire up irb and load the socket library, like so:
kawaii:~ craig$ irb
irb(main):001:0> require 'socket'
=> true
Then we open the socket, passing in a block of work.
irb(main):002:0> TCPSocket.open('xeriom.net','smtp') do |t|
irb(main):003:1* t.gets
irb(main):004:1> end
=> "220 pluto.xeriom.net ESMTP Postfix\r\n"
Lovely, simple, elegant. Yum! Checking if a socket is listening is easy as well. Here, we already have the socket library loaded from above so…
irb(main):005:0> begin
irb(main):006:1* t = TCPSocket.new('xeriom.net',8000)
irb(main):007:1> t.close
irb(main):008:1> rescue
irb(main):009:1> "Error: socket not open"
irb(main):010:1> end
=> "Error: socket not open"
Boom, simple. Perhaps a little too simple; now I have nothing to do except tidy…
Tomorrow I’ll play with opening many sockets simultaneously, checking if each one is open or closed. Bonus points if you beat me to it, or if you can make Java look as nice :)
curl -LO http://barkingiguana.com/2011/03/01/socket-programming-in-ruby.html.orig
curl -LO http://barkingiguana.com/2011/03/01/socket-programming-in-ruby.html.orig.asc
gpg --verify socket-programming-in-ruby.html.orig{.asc,}
If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.