Does Threading work properly?

Hiya

Thanks - I just about worked that out - lol sorry - I now have another
threading problem though:

require 'net/telnet'
require 'thread'

require 'net/telnet'
tn = Net::Telnet.new('Host' => 'ancient.anguish.org',
'Port' => '2222',
'Telnetmode' => true) { |str| print str }

tn.cmd("String" => "guest") { |str| print str }

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

thr4.join

I have one thread to enter commands into the server and one thread to
listen to all the server repsonses that are going on all the time.

But this code here - it lets me enter 1 command - then it just listens
and I cant enter anymore commands

Any clue?

Thanks
Kingsley

Hi

When I do this:

thr3 = Thread.new() do
loop do
puts \"thread\"
end
end

The loop only lasts for about 5 seconds - printing out
\'thread\' about
100 times

To answer the question in the subject: yes, threading does work
properly :slight_smile:

When the main thread of your program reaches the end, all other
threads are
killed too.

Add the following line at the end:

thr3.join

(which means "wait for thr3 to finish", which of course it never
does).

···

On 06-30-2003 05:20 pm, you wrote:

On Tue, Jul 01, 2003 at 01:12:50AM +0900, kingsley@icecode.org wrote:

Regards,

Brian.

require 'net/telnet'
require 'thread'

Why do you keep putting these backslashes in? It means your code can’t be
pasted in and run :frowning:

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.

···

On Tue, Jul 01, 2003 at 02:07:14AM +0900, kingsley@icecode.org wrote:

From: Brian Candler [mailto:B.Candler@pobox.com]
Sent: Monday, June 30, 2003 3:17 PM
To: ruby-talk ML
Subject: Re: Does Threading work properly?

require 'net/telnet'
require 'thread'

Why do you keep putting these backslashes in? It means your code can’t be
pasted in and run :frowning:

I think it’s his mail client; IIRC I asked him about that some time ago.
Very irritating and unnecessary, IMO.

···

On Tue, Jul 01, 2003 at 02:07:14AM +0900, kingsley@icecode.org wrote: