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

I am reading the Perl cookbook and trying the example of bidirectional client, however it doesn't work. it only outputs one line responded back from the server, please advise:
use strict; use IO::Socket; my ($line, $obj, $pid, $input); $obj = IO::Socket::INET->new(PeerAddr => "some.com", PeerPort => 7777, Proto => "tcp", Timeout => 60, ); die "cant fork" unless (defined ($pid=fork())); if ($pid) { while (defined ($line = <$obj>)) { print STDOUT $line;} kill 9, $pid; } else { while (defined ($input = <STDIN>)) { print $obj $input; } }

Replies are listed 'Best First'.
Re: bidirectional client
by belg4mit (Prior) on Nov 25, 2001 at 07:10 UTC
    My hunch is you're lucky to be getting that. You probably shouldn't have removed the auto-flush line. Of course we don't know if you're trying to bicirectional line-by-line (a la expr or cat, autoflush is a must for this kind of talking). If not and you're only doing something like HTTP/1.1 you still really ought to have autoflush but it's not the same. As you would get a multi-line output from child per single input from you (as opposed to one-to-one which is what you don't specify).

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

      I am not sure. I DID test it with autoflush turned on. same result. (IIRC, autoflush by default is on in recent IO::Socket). The server side in this case is sorta like SMTP.
        As of VERSION 1.18 all IO::Socket objects have autoflush turned on by default. This was not the case with earlier releases.
        Is some.com what you are actually trying to connect to? I get connection refused (I'd rather not mock up my own server that may or may not be anything like what is in your environment). A sample transcript (data entered on STDIN) might prove useful as well.

        I changed PeerPort to 80 and enter GET / HTTP/1.0 and receive multiple lines as a response.

        --
        perl -p -e "s/(?:\w);([st])/'\$1/mg"

IO::Select
by IlyaM (Parson) on Nov 26, 2001 at 07:34 UTC
    If you need bidirectional client it makes sense to use IO::Select. With select you can avoid forking child process. In real application it can simplify code a lot since you don't need any IPC between parent and child processes.