Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Closing socket handle of child process

by paulc1976 (Novice)
on Jan 02, 2012 at 23:28 UTC ( [id://945977]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys! I need some advice on how to close a socket handle on a child process, I have a daemon which accepts new connections and forks off a child process to handle them. I need communication to be bidirectional, so I've tried forking again to have the parent listening and the child writing to the same socket. The listening parent is just used to listen for a command to stop the connection which kills the child process but the socket associated with it keeps sending. So to sum up after a socket is accepted on the child it calls the client connect method and forks again (code shown below)

sub client_connect { my($connection) = @_; my $kidpid; die "can't fork: $!" unless defined($kidpid = fork()); if ($kidpid) { # parent copies the socket to standard output while (<$connection>) { last if /Close_Server/; } kill("TERM" => $kidpid); # send SIGTERM to child + } else { while (1) { print $connection "some output\n"; } continue { sleep 10; } } }

Replies are listed 'Best First'.
Re: Closing socket handle of child process
by ikegami (Patriarch) on Jan 03, 2012 at 00:22 UTC

    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?

      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.

      - tye        

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://945977]
Approved by johngg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found