Perl Sockets Parent/Child Problem

Discussion in 'Perl Misc' started by Ken Browning, Feb 12, 2004.

  1. Ken Browning

    Ken Browning Guest

    (Source at end of question)

    Given this source, I am able to connect to the server, but when I try
    to send data to the child process, it never seems to get there...any
    suggestions?

    use IO::Select;
    use IO::Socket;
    ##############################################################

    my ($host, $port, $kidpid, $handle, $line);

    unless (@ARGV == 2) { die "usage: $0 host port" }
    ($host, $port) =@ARGV;

    $handle=IO::Socket::INET->new (Proto => "tcp",
    PeerAddr => $host,
    PeerPort => $port)
    or die "can't connect to $port on $host: $!";

    $handle->autoflush(1);

    print STDERR "[Connected to $host:$port]\n";

    #die "can't fork: $!" unless defined($kidpid=fork());
    $kidpid = open (KID_TO_WRITE, "|-");
    unless (defined $kidpid) {
    die "can't fork: $!"
    }

    if($kidpid){ #parent process
    print $handle "0=1.3\n";
    printf("sent: 0=1.3\n");
    while (defined ($line = <$handle>)) {
    chomp($line);
    printf("RECV: %s\n",$line);
    printf( KID_TO_WRITE "test %s\n",$line);
    print KID_TO_WRITE "[this is a test]" ;
    printf( "printed to child process...: %s\n",$line);
    }
    kill ("TERM" => $kidpid);
    }else{ #child process
    printf( STDERR "Inside child process...\n");

    while (defined ($line = <STDIN>)) {
    print STDERR "Inside loop : $line";
    print STDERR "SEND: $line";
    }
    printf( STDERR "Exiting child process...\n");
    exit;
    }
    exit;
     
    Ken Browning, Feb 12, 2004
    #1
    1. Advertising

  2. (Ken Browning) writes:

    > Subject: Re: Perl Sockets Parent/Child Problem


    I don't think sockets are relevant. You should always try to reduce
    your problem to it's mimimal form. Did you try just the parent/child
    part on it's own?

    > Given this source, I am able to connect to the server, but when I try
    > to send data to the child process, it never seems to get there...any
    > suggestions?


    I suggest not killing the child before the data gets there.

    > $kidpid = open (KID_TO_WRITE, "|-");


    > while (defined ($line = <$handle>)) {
    > chomp($line);
    > printf("RECV: %s\n",$line);
    > printf( KID_TO_WRITE "test %s\n",$line);
    > print KID_TO_WRITE "[this is a test]" ;
    > printf( "printed to child process...: %s\n",$line);
    > }
    > kill ("TERM" => $kidpid);


    You never unbuffered KID_TO_WRITE so at this point all the data is
    probably in a buffer.

    You should simply close KID_TO_WRITE once you've written the data to it.
     
    Brian McCauley, Feb 12, 2004
    #2
    1. Advertising

  3. Ken Browning

    Ben Morrow Guest

    Ken Browning <> wrote:
    > Thank you for your input....I agree that sockets have nothing to do
    > with it, but my program is a socket program, thus the title...


    But your problem is not a socket problem, hence the subject was wrong.

    > I tried close(KID_TO_WRITE) (after writing to it) and that flushed the
    > buffer and the child process received the data. What I need to do is
    > keep that open until I have received a close indication from the
    > server. Is there a way to flush that buffer _without_ closing
    > KID_TO_WRITE?


    Put
    select((select(KID_TO_WRITE), $|=1)[0]);
    before you do the print.

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Feb 12, 2004
    #3
  4. Ken Browning <> wrote:


    > Is there a way to flush that buffer _without_ closing

    ^^^^^
    ^^^^^

    Are you being serious?



    perldoc -q flush

    How do I flush/unbuffer an output filehandle? Why must I do this?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 13, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. davout
    Replies:
    0
    Views:
    554
    davout
    Apr 18, 2004
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,139
    David Schwartz
    Dec 9, 2003
  3. Replies:
    0
    Views:
    213
  4. Noel Dolan
    Replies:
    0
    Views:
    242
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    153
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page