![]() |
File handlings
Hi !
My B program is a simple script, which reads a string from STDIN, converts it to uppercase and prints it to STDOUT. B.pl #!/bin/perl use strict ; my $string=<STDIN>; print STDOUT uc $string,"\n" ; Now, I have A program, which listens on a TCP socket. Whenever string come over socket, it should be put on STDIN buffer (yes, STDIN), forking a new child, redirecting the parent SOCKET to child's STDOUT and exec B.pl. (The same way Apache & CGI modules work). I was trying to use select() but no luck. A.pl #!/bin/perl use strict ; .... SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE .... TCP settings.... ....... if (my $pid=fork) { waitpid($pid,0); # Ok, here is parent } else { my $string="This must be put in STDIN buffer" ; << Don't know how my $old=select(SOCKET) ; << This is not working exec("A.pl"); # and child select $oldstdout ; } Any help would be appreciated |
Re: File handlings
Glodalec <glodalec@yahoo.com> wrote: > Now, I have A program, which listens on a TCP socket. Whenever string > come over socket, it should be put on STDIN buffer (yes, STDIN), forking > a new child, redirecting the parent SOCKET to child's STDOUT and exec > B.pl. (The same way Apache & CGI modules work). I was trying to use > select() but no luck. No, that's not how filehandles work at all. You can't push stuff back onto them: if you want to pass stuff to your child you have to use a pipe. > A.pl > #!/bin/perl > use strict ; > ... > SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE > ... > > TCP settings.... > ...... > if (my $pid=fork) fork can fail, in which case it returns undef. > { > waitpid($pid,0); # Ok, here is parent > } else > { > my $string="This must be put in STDIN buffer" ; << Don't know how > my $old=select(SOCKET) ; << This is not working > exec("A.pl"); # and child > select $oldstdout ; > } > > Any help would be appreciated my $pid = open my $TOCHILD, '|-', 'A.pl' or die "fork failed: $!"; print $TOCHILD "The child will read this on its STDIN"; See perldoc -f open. Ben -- For the last month, a large number of PSNs in the Arpa[Inter-]net have been reporting symptoms of congestion ... These reports have been accompanied by an increasing number of user complaints ... As of June,... the Arpanet contained 47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] * ben@morrow.me.uk |
Re: File handlings
In article <bvoe9t$t31$1@wisteria.csv.warwick.ac.uk>,
usenet@morrow.me.uk says... > > Glodalec <glodalec@yahoo.com> wrote: > > Now, I have A program, which listens on a TCP socket. Whenever string > > come over socket, it should be put on STDIN buffer (yes, STDIN), forking > > a new child, redirecting the parent SOCKET to child's STDOUT and exec > > B.pl. (The same way Apache & CGI modules work). I was trying to use > > select() but no luck. > > No, that's not how filehandles work at all. You can't push stuff back > onto them: if you want to pass stuff to your child you have to use a > pipe. Had never used them (except on command line), will read about them. > > > A.pl > > #!/bin/perl > > use strict ; > > ... > > SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE > > ... > > > > TCP settings.... > > ...... > > if (my $pid=fork) > > fork can fail, in which case it returns undef. I know, this script has been arranged a lot when posting here. > > > { > > waitpid($pid,0); # Ok, here is parent > > } else > > { > > my $string="This must be put in STDIN buffer" ; << Don't know how > > my $old=select(SOCKET) ; << This is not working > > exec("A.pl"); # and child > > select $oldstdout ; > > } > > > > Any help would be appreciated > > my $pid = open my $TOCHILD, '|-', 'A.pl' > or die "fork failed: $!"; > > print $TOCHILD "The child will read this on its STDIN"; THanks for advice. How about a child exec, which should actually write to parent's SESSION handler (got from accept()), instead of its own STDOUT.? > > See perldoc -f open. > > Ben > > |
Re: File handlings
Glodalec <glodalec@yahoo.com> wrote: > > print $TOCHILD "The child will read this on its STDIN"; > THanks for advice. How about a child exec, which should actually write > to parent's SESSION handler (got from accept()), instead of its own > STDOUT.? It's probably easiest to do that manually, viz (untested): accept my $NEWCONN, $LISTENER or die "accept failed: $!"; # or you'd probably be better off using IO::Socket::INET my $NEWCONN = $LISTENER->accept or die "accept failed: $!"; pipe my ($FROMPARENT, $TOCHILD) or die "pipe failed: $!"; my $pid = fork; defined $pid or die "fork failed: $!"; unless ($pid) { open STDIN, '<&', $FROMPARENT or die "dup2 to STDIN failed: $!"; open STDOUT, '>&', $NEWCONN or die "dup2 to STDOUT failed: $!"; exec 'A.pl' or die "exec failed: $!"; } close $FROMPARENT; close $NEWCONN; Obviously you want to put all this in a sub, so you can say my $TOCHILD = accept_new_child($LISTENER); If you get errors from the open '<&' syntax, you may need a newer version of perl; or you can replace them with open STDIN, '<&' . fileno $FROMPARENT or ... 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. ~ ben@morrow.me.uk ~ Jorge Luis Borges, 'The Babylon Lottery' |
| All times are GMT. The time now is 07:05 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.