From: mike@... (Mike Stok) Date: 2002-04-06T00:34:17+09:00 Subject: Re: socket question In article , Markus Jais wrote: > hello > > lets assume that I have a server on localhost which returns a string. > but I have no control about the server and it does not to > seem to return somethink like and EOF. > > my sample client works perfectly in Perl but has a problem > in Ruby (and I prefer Ruby, of course) > > the Perl client: >================ > use strict; > use IO::Socket; > > my $socket = IO::Socket::INET->new('localhost:5533') or die "Error: $!\n"; > print $socket "create:10027566:drizzt\n"; > my $str = <$socket>; > print "$str"; > close($socket); > > > the Ruby client: >================ > require "socket" > port = 5533 > host = "localhost" > > t = TCPSocket.new(host, port) > t.print("create:10027566:drizzt\n") > t.readlines.each do |line| > puts line > end > t.close > As you can see, I interrupted the Ruby client with Ctrl-C > because there was no output. I thought that readlines > would work as Perls $str = <$socket>. Here you see perl's context dependency, <> knows that it's being used in a scalar assignment so just slurps in a single line I would guess that readlines is reading all the available input before letting each iterate, so maybe the connection isn't closed. > Any ideas how to improve the Ruby script ?? does t.each_line do |line| ... end help at all? Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@starnix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.starnix.com/ | 75D2 9EC4 C1C0 0599 13DA