From: Brian Candler Date: 2003-07-01T05:17:00+09:00 Subject: Re: Does Threading work properly? On Tue, Jul 01, 2003 at 02:07:14AM +0900, kingsley@icecode.org wrote: > require \'net/telnet\' > require \'thread\' Why do you keep putting these backslashes in? It means your code can't be pasted in and run :-( > thr4 = Thread.new() do > loop do > tn.cmd(gets.chomp) { |str| print str } > end > end > > thr3 = Thread.new() do > loop do > tn.waitfor(\"Match\" => /.+/) { |str| print str } > end > end I have never used the telnet module you are talking about; are you sure it's thread-safe? (i.e. you can simultaneously call "cmd" and "waitfor" on a single instance?) Looking at the documentation in the pickaxe, it looks like you're using it in the wrong way: if you pass a block to Telnet#cmd then that block receives the results from the server. Hence you have two threads simultaneously trying to read responses from the server, which seems like a bad thing to me. You'd need to do something like: line = gets.chomp tn.cmd( {'String'=>line, 'Match'=>/.+/, Timeout=>5} ) But even that's wrong, because the server could generate multiple responses to one command, but your match on /.+/ would catch only the first response. In other words, the telnet class is only really useful if you know what response to expect from each command. It's a kind of poor-man's "expect". Personally I think you'd be better of with a raw socket, because then you definitely _can_ send and receive simultaneously: require 'socket' require 'thread' sock = TCPSocket.new('ftp.tiscali.nl',21) t1 = Thread.new do while line = gets sock.write line end end t2 = Thread.new do while ch = sock.read(1) putc ch end end t1.join t2.join The only thing to beware of is that a true telnet server may send some odd escape sequences as it tries to negotiate some telnet options, but apart from that the above should be fine as a simple telnet client. Regards, Brian.