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

Hi guys, I have a socket set up with IO::Socket::INET. It works in that I am able to write to the socket, however I can't capture what the remote host sends back. I just get nothing. I know the connection works, because I can use telnet to drive the dialogue manually and when I send the 'status' request, I get a dump of data back. How can I get my socket to capture this so that I can do something useful with it? Here's a snip of the code I'm using:
my $sock = new IO::Socket::INET ( PeerAddr => "$hostname", PeerPort => "$port", Blocking => 0, Proto => 'tcp', ); print $sock "Action: Login\r\n"; print $sock "UserName: $foo\r\n"; print $sock "Secret: $bar\r\n\r\n"; print $sock "Action: status\r\n\r\n"; $/="\r\n"; open(FH,">>/home/chris/statusdump.txt"); while(my $data=<$sock>){ $data =~ /[a-z]/ || next; print FH $data; } close FH;
I have opened up the permissions on that temporary file and I'm tail -f'ing it to see what comes in, but the file doesn't grow at all. What's the obvious mistake please? thanks christo

Replies are listed 'Best First'.
Re: Problems reading back from socket
by sgifford (Prior) on Nov 16, 2005 at 16:47 UTC
    Why are you making your TCP socket nonblocking (with the Blocking => 0 parameter)? A nonblocking socket will return if there's nothing to read; I suspect that when you try to read it with my $data=<$sock>, there's no data to read, so it returns false and the whole program exits. I suspect removing this parameter would solve your problem, but I haven't tested it.

    You could confirm this by adding debugging output to your program, or running it in the Perl debugger.

    I'd also recommend adding more error checking; right now you wouldn't notice if the socket creation failed, or the prints to the socket failed, since you're not checking their return values. Checking the return value from close also sometimes shows errors that were delayed, or caused the socket to close.

Re: Problems reading back from socket
by vek (Prior) on Nov 17, 2005 at 06:50 UTC

    Is it possible that the remote server has not recognized that you've finished sending data? Have you tried issuing a more forceful method of indicating you're done via shutdown?

    $sock->shutdown(1); # done writing but still open for reading

    Do you have access to the remote server? Can you verify that your data is received correctly and that the remote server is actually attempting to send you data back?

    -- vek --
Re: Problems reading back from socket
by rupesh (Hermit) on Nov 16, 2005 at 13:00 UTC
    A basic client and server, socket implementation(untested).

    Server:
    use strict; use IO::Socket; my $sock = new IO::Socket::INET (LocalHost => 'localhost', LocalPort => 5004, Proto => 'tcp', Listen => 5, Reuse => 1 + ); die "Socket could not be created. Reason: $!" unless $sock; my ($new_sock, $buf); while ($new_sock = $sock->accept()) { print "Connection from ", $new_sock->peerhost, ": on port ", $new_s +ock->peerport,"\n"; print $new_sock "Hello from the server"; while (defined ($buf = <$new_sock>)) { print $buf; } } close ($sock);
    Client:
    use strict; use IO::Socket; my $line; my $socket = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '5004', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $socket; $socket->recv($line, 80); print "$line"; print $socket "Hello from the Client!\n"; close($socket);


    Cheers,
    Rupesh.
A reply falls below the community's threshold of quality. You may see it by logging in.