• OPEN SOURCE SCHOOL

snuxoll.com - Rants of a web designer

  • snuxoll.photos
  • snuxoll.jaiku
  • snuxoll.identica
  • snuxoll.store
  • ircwall
Home

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Recent comments

  • Yes, my socket code is
    3 weeks 6 days ago
  • Yes, I've been putting it off
    4 weeks 1 day ago
  • Good work
    4 weeks 2 days ago
  • @thread isn't made until
    5 weeks 5 days ago
  • rick
    5 weeks 5 days ago
  • Not that I'm aware of, but
    5 weeks 5 days ago
  • GServer
    5 weeks 5 days ago
  • mandriva
    6 weeks 9 hours ago
  • Unfortunately that doesn't
    6 weeks 16 hours ago
  • comes with music
    6 weeks 21 hours ago

Blogroll

Profarius Blog
DrDerek's Blog

Threaded Networking in Ruby

snuxoll — Fri, 10/10/2008 - 21:04

    Today in #ruby on Freenode someone who was working on an IRC bot was showing off their code and pointed out that it couldn't maintain more than one connection at a time. While I already had a IRC specific chunk of code I used myself to show him, I figured a more general solution would be a good idea. As such I figured I'd encapsulate a network connection in a simple class, so here you go!

# General purpose wrapper for network connections
class NetworkConnection
  attr_reader :hostname, :port, :thread
  # Creates a new instance of NetworkConnection with the
  # specified hostname and port
  def initialize(hostname, port)
    @hostname = hostname
    @port = port
  end
  # Connects to the specified hostname and port given in new
  def connect
    @thread = Thread.new do
      @socket = TCPSocket.new(@hostname, @port)
      @socket.each { |line|  @block.call(line) }
    end
  end
  # Closes the socket
  def disconnect
    @socket.close
  end
  # Takes a block of code to be called on each line recieved
  def on_recv(&block)
    @block = block
  end
  # Sends text to the socket
  def send(text)
    @socket.send(text + '\r\n', 0)
  end
end

So now, the usual sample usage!

connection = NetworkConnection.new("irc.freenode.net", 6667)
connection.on_recv { |line| puts line }
connection.connect

Obviously this sample code will exit right after the connection is made, since the connection is maintained in a separate ruby thread. I'll leave it up to you to actually find some use in this, but there you go.

  • networking
  • ruby
  • socket

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

GServer

Anonymous — Sat, 10/11/2008 - 02:50

Doesn't Ruby's included GServer library do something similar?

  • reply
snuxoll's picture

Not that I'm aware of, but

snuxoll — Sat, 10/11/2008 - 03:47

Not that I'm aware of, but this was meant to be a simple way to show how to do threaded networking in ruby apps, so take it for what it's worth.

----
Stefan Nuxoll

  • reply

rick

Anonymous — Sat, 10/11/2008 - 11:26

what is @thread's initial value? It's not mentioned anywhere else in the class.

  • reply
snuxoll's picture

@thread isn't made until

snuxoll — Sat, 10/11/2008 - 11:50

@thread isn't made until NetworkConnection#connect is called, and when it is created it's the instance of the worker thread that listens to the socket for incoming data.

Edit: Aaah, I see why you asked that, I made a mistake in my code.

@thread << Thread.new do

should be
@thread = Thread.new do

I have corrected it in the post now.

----
Stefan Nuxoll

  • reply

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

Search

Random Images

10162008212 07302008194 10222008001 07042008143 10282008012 10282008010 07042008150 10182008215 07302008192 07302008196 04132008064 (Modified) 20081116025

Latest from snuxoll

Google Analytics
The Starting Line – Direction
Moving Elements in a Ruby Array by Name
Cat says it's bedtime, I think I'll listen to her.
Screenshot6 (screenshots)
More from snuxoll

Recent MP3 Purchases

Support Your Blogger

Hey, I need to eat too, chip in a dollar or chip in twenty, it's up to you.
  • snuxoll.photos
  • snuxoll.jaiku
  • snuxoll.identica
  • snuxoll.store
  • ircwall

Content Copyright ©2008 Stefan Nuxoll