Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl Sockets Parent/Child Problem

Reply
Thread Tools

Perl Sockets Parent/Child Problem

 
 
Ken Browning
Guest
Posts: n/a
 
      02-12-2004
(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;
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      02-12-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      02-12-2004

Ken Browning <(E-Mail Removed)> 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.
~ (E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-13-2004
Ken Browning <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A noob playing with: Divert sockets, Linux and Perl. vivek.m1234@yahoo.com Perl Misc 0 04-24-2008 12:56 PM
Perl and Sockets Zev G. Perl Misc 3 08-02-2007 04:29 PM
Send an integer value through sockets in perl. Tompyna Perl Misc 2 02-17-2004 09:27 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
Large File transfers using sockets in Perl or any method if available SRam Perl Misc 1 08-22-2003 01:20 PM



Advertisments