c0bra has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I realize that there's been a few very similar threads on hanging sockets but none of the fixes that worked for others seem to work for this issue, so I thought I'd add it to the heap.

I've got a POE TCP server running on localhost on a Windows box with ActivePerl 5.8.8. A pure-POE client-server setup works fine. Both sides can read/write. However on the client side I just want a simple connect->write->read->disconnect. No need for the extra event handling. So I tried using IO::Socket::INET to do it. The problem is, no matter what I do the script hangs while trying to read from the socket. I've tried reading with <$socket> in a loop. I've tried it outside of a loop. I've tried sysread($socket, $buf, 1024) in and out of a loop. I've tried doing shutdown($socket, 1). I've tried setting $/ to some other string and passing it through the server and it still hangs. Right now the code looks like this:
use IO::Socket::INET; $socket = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 5555, Proto => 'tcp', Type => SOCK_STREAM, Timeout => 5, ) or die $@; $socket->autoflush(1); print $socket "Hello!\n"; my $line = ""; sysread($socket, $line, 1024); print "ANSWER: " . $line; close($socket);
The server receives the "Hello!" and responds, but the client hangs on the sysread without ever receiving any data.

Thanks for taking the time to read this.

Replies are listed 'Best First'.
Re: IO::Socket hanging in ActivePerl 5.8.8
by Anonymous Monk on Apr 27, 2006 at 22:49 UTC
    This is just a wag... <$socket> is going to buffer (and hang) until Perl gets what it's expecting for an end of line char. Mabye the server didn't end it's reply with a \n and <$socket> is still waiting for it? Mixing 'print' and 'sysread' on the same filehandle is not recommended, and maybe the reason why this is hanging as well. Maybe replacing print with syswrite will get things flowing again.
Re: IO::Socket hanging in ActivePerl 5.8.8
by cdarke (Prior) on Apr 27, 2006 at 22:36 UTC
    No other replies, so let's have a think. Are you certain the server is sending the data and is unbuffered? <$socket> should work, but it's using $/ as a record terminator, so expects (normally) a "\n", or "\r\n" on Windows. Can't say I've used sysread for sockets, you could try recv instead.
Re: IO::Socket hanging in ActivePerl 5.8.8
by demerphq (Chancellor) on Apr 28, 2006 at 07:29 UTC

    Im wondering if this could line ending related. Are you sure you are actually sending the correct line ending? Sockets are binmoded by default from what I remember so perl wont do any line ending translations. This is just a wild guess based on some work I did with browserUk on the IO test suite.

    ---
    $world=~s/war/peace/g

Re: IO::Socket hanging in ActivePerl 5.8.8
by chromatic (Archbishop) on Apr 27, 2006 at 23:59 UTC

    It surprised me not to see an accept() call in there for a TCP connection. I'm rushing off elsewhere now, but I thought you had to have a client connection to read. (I mean, how do you know you have a client connected without polling the socket somehow?)

    Update: Well of course not, this is the client as your post says! Now I shall get the sleep I needed eight hours ago.

      Because the server is receiving the data the client sends to it?
Re: IO::Socket hanging in ActivePerl 5.8.8
by c0bra (Acolyte) on Apr 28, 2006 at 00:02 UTC
    The server is definitely ending its data with \n. I made sure of that. Unless POE is somehow stripping it out, but I don't think it'd do that.
      You could always try connecting to the server with telnet to see what it is sending.
Re: IO::Socket hanging in ActivePerl 5.8.8
by c0bra (Acolyte) on Apr 28, 2006 at 03:07 UTC
    Looks like it works fine on bsd.
      I don't think this really matters, but you could try coding the socket the long way (no IO::Socket::INET) on the off chance that something is goofed there. You might also try closing the send connection with shutdown() before attempting to read on the chance that it might clear something.
        I tried doing shutdown($socket, 1) before reading but the client got back no data at all.
Re: IO::Socket hanging in ActivePerl 5.8.8
by c0bra (Acolyte) on Apr 28, 2006 at 14:38 UTC
    Fantastic. Well it's working now, but I really have no idea what I did to fix it :\

    The POE server is flushing all its output at once so the client gets everything back on sysread. <$socket> is working now too.

    I'm going to assume I did something horrible on the server side that was causing it to hang. Thanks for your help everyone.