Socket hangs when I read after writing!

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.” [email protected]
wrote
in message news:%[email protected]

…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…

2009/3/16 Just Another Victim of the Ambient M.
[email protected]:

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

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

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!