The listening parent is just used to listen for a command to stop the connection which kills the child process
You want the child to send a command to the parent to kill that same child? Isn't that a complicated way to just exit?
| [reply] [d/l] |
exit isn't complicated. Waiting to read a command while busy writing output, in Perl, certainly can be complicated. Given that sockets are being used here, select would likely be a useful solution. But fork is equally useful and likely makes for a simpler solution (well, at least if you aren't using a Perl that emulates fork(), such as non-Cygwin Windows Perl does).
I'd be curious what doesn't work about the presented code. I don't see any obvious problem with it. And I don't see where the failing of the code is described.
Also, what the OP calls the "listening parent" seems to me likely (based on the code and on other parts of the description) to actually be the middle process that is reading (not "listening") while the parent of that middle process is the one listening for new connections to come in (and the middle's child, the grandchild process, is the one writing).
Certainly, the code presented is incomplete. But I fail to imagine a likely problem with the grandparent process that would likely point the OP to the problem being with the code presented.
paulc1976, please describe, precisely, how your code fails to behave as you desired. And please provide more code. Best would be to pare down the other code so that you have a minimalist, self-contained example that others can run to observe how it fails to meet your described desired requirements.
| [reply] |
Thank you for your reply, I didn't post the entire code as I didn't want to take up too much space, instead of posting the entire daemon code, I thought it might be better to post the code for the helper program.
This program (helper) acts as an intermediary between my Perl TK program and a forking server which accepts connection requests from helper. helper also has a bidirectional pipe connected to communicate with the TK program.
My aim is to send a text command to the helper such as Close_Server which when detected would close the socket connection with the forking server. The problem was that due to blocking I couldnt implement listening to STDIN (this is the piped output from TK program) and listening to the socket in the same while loop without using select (as you suggest). I've tried to use fork which works great but killing the child process does not close the socket, the data still keeps coming through. The only way to stop this is by killing the entire process.
#!/usr/bin/perl
#
# IP administrator helper.
#
use 5.005;
use IPADM;
use IO::Socket;
use strict;
use vars qw/$sock/;
do {print "Usage: helper host port\n"; exit} unless @ARGV == 2;
my $neighbourSocket = join ":", $ARGV[0], $ARGV[1];
my $int_count;
#Flush back to parent
STDOUT->autoflush(1); # unbuffer output
$SIG{PIPE} = sub {print "2 Pipe Error.\n$EOF\n"; exit;};
$SIG{TERM} = sub {exit; };
#ignore child processes to prevent zombies
$SIG{CHLD} = 'IGNORE';
my $sock = IO::Socket::INET->new( PeerAddr => $ARGV[0],
Proto => 'tcp', PeerPort => $ARGV[1]);
print +((defined $sock) ? "0 Connect OK:$$" : "3 Connect Failed:$$"),
+"\n$EOF\n";
my $kidpid;
# split the program into two processes, identical twins
die "can't fork: $!" unless defined($kidpid = fork());
# if parent
if ($kidpid) {
# parent copies the socket to standard output
#Read from parent (STDIN), this is received from TK program vi
+a a pipe
while(<STDIN>) {
# if the close server command from TK program is detected,
+ break from loop
last if /Close_Server/;
}
# kill the child process
kill("TERM" => $kidpid);
}
# if child
else {
# Loop forever until socket is closed (I cant close it!!)
while (1) {
# Create array to collect output from server
my(@data) = ();
# Add the addressing information to start of array
push @data, "$neighbourSocket\n";
#collect data from daemon until EOF is reached, at this po
+int it can be piped back to TK program
while (<$sock>) {
push @data, $_; # accumulate command's reply
last if /^$EOF$/;
}
# Catch daemon failure, send output back to TK program for
+processing
print (/^$EOF$/ ? @data : "4 Daemon Failure\n$EOF\n");
} # whilend
}
close $sock;
exit;
Any guidance would be much appreciated, Paul | [reply] [d/l] |