Forum: Ruby Socket hangs when I read after writing!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Just Another Victim of the Ambient M. (Guest)
on 2009-03-16 06:50
(Received via mailing list)
I have the following Ruby program:


sock = TCPSocket.new('host.com', 12345)

sock.write('This is a test')

data = sock.read(1024)
puts data


    ...The service this connection connects to just reads the string and
write a response which this program then tries to read.  I expected it
to
just work but instead it just hangs.  The weird part is that it hangs
before
the data is actually written out through the socket.  The service on the
other end doesn't get any data.  If I remove the end part of the program
that attempts to read the response, the data makes it through and is
read
correctly by the service.
    What is wrong with my program?  Why is it hanging?
    Thank you...
Just Another Victim of the Ambient M. (Guest)
on 2009-03-16 11:35
(Received via mailing list)
"Just Another Victim of the Ambient M." <removed_email_address@domain.invalid>
wrote
in message news:%Qkvl.43684$removed_email_address@domain.invalid...
>
>    ...The service this connection connects to just reads the string and
> write a response which this program then tries to read.  I expected it to
> just work but instead it just hangs.  The weird part is that it hangs
> before the data is actually written out through the socket.  The service
> on the other end doesn't get any data.  If I remove the end part of the
> program that attempts to read the response, the data makes it through and
> is read correctly by the service.
>    What is wrong with my program?  Why is it hanging?
>    Thank you...

    Okay, I can shed some more light on this problem.
    If the service I'm connecting to is the first one to send data and
my
program reads that data like so:


sock = TCPSocket.new('host.com', 12345)

line = sock.readline
puts line


    ....even if the service sends several lines, sock.readline won't
return
until the connection is closed.  This sounds like it may be a bug
specific
to the Win32 implementation of Ruby 1.8 but that's just a guess.
    Does anyone know what's going on here?
    Thank you...
Eleanor McHugh (Guest)
on 2009-03-16 13:48
(Received via mailing list)
On 16 Mar 2009, at 09:32, Just Another Victim of the Ambient M.
wrote:
>
>
>    ....even if the service sends several lines, sock.readline won't
> return
> until the connection is closed.  This sounds like it may be a bug
> specific
> to the Win32 implementation of Ruby 1.8 but that's just a guess.
>    Does anyone know what's going on here?

I'll see if I can find time to look at this properly later, however
looking at this snippet I have to ask if readline is what you really
want to be doing: i.e. is the server sending messages which are
terminated with a newline? Because if not sock.readline will just sit
there buffering input from the server until the socket is closed.

BTW, if you follow the link in my .sig file you'll find several
presentations with lots of Ruby socket code in them which might help
with your experiments. None of them have been tested on Windows so
YMMV but they should be good for general inspiration.


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
----
raise ArgumentError unless @reality.responds_to? :reason
Robert K. (Guest)
on 2009-03-16 14:18
(Received via mailing list)
2009/3/16 Just Another Victim of the Ambient M.
<removed_email_address@domain.invalid>:
>> data = sock.read(1024)
>>    What is wrong with my program?  Why is it hanging?
> puts line
>
>
>    ....even if the service sends several lines, sock.readline won't return
> until the connection is closed.  This sounds like it may be a bug specific
> to the Win32 implementation of Ruby 1.8 but that's just a guess.
>    Does anyone know what's going on here?
>    Thank you...

Can you post the code of the other side?  Maybe the other side does
not flush the output because you do not close the write channel and
the other side still expects more input.  There are plenty of other
things that could go wrong but seeing only this bit of code it is
difficult to come up with more helpful replies.

Cheers

robert
Just Another Victim of the Ambient M. (Guest)
on 2009-03-17 00:15
(Received via mailing list)
Thank you to both Robert and Eleanor McHugh for your help and
inspiriation.
    It looks like my problem was that, while I was vigilant about
flushing
the socket on the client side, I wasn't so careful on the server side.
I
may be back here with more problems but things look like they're working
as
expected while I'm flushing the socket on the other end.
    The reason why I didn't post the server side was because it's
written in
Python and I thought it would have been a bit off-topic here, even
though
many programmers here are also familiar with that language.
    Again, thank you all for your help!
This topic is locked and can not be replied to.