From: Jason Wold Date: 2004-04-25T04:16:25+09:00 Subject: Re: Playing with sockets... Here is something I ripped out of some code I am working on. require 'socket' require 'thread' PORT=4033 io = Proc.new{ TCPServer.new('localhost', PORT).accept } #io = Proc.new { IO.new(1) } mutex = Mutex.new threads =[] cmd_buffer = [] threads << Thread.new do cmd = "" puts "Started thread" if $DEBUG # call the proc object that creates the io object f = io.call while ! cmd.match(/^quit|q/i) f.print "HA>" f.flush cmd = f.readline.chomp next if cmd.empty? puts "Got command #{cmd}" if $DEBUG (f.close; next) if cmd.match(/^(quit|q)$/i) # lock access to cmd_buffer since another thread will # be reading it mutex.synchronize {cmd_buffer << cmd } end end threads.each { |t| t.join } __END__ On the server: =========== $ ruby -d test Started thread On the client: =========== $ telnet ital.jasonandvivian.net 4033 Trying 192.168.168.20... Connected to ital. Escape character is '^]'. HA>testing HA>this is a test HA>quit Connection closed by foreign host. On the server: =========== Got command testing Got command this is a test Got command quit > > I wonder: How hard is it to write a server to which one would connect > with telnet? > > Hal > >