Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Child reaping update:

After yet more experimentation with this fabulously educational node I did what was necessary to make it run via init.d based on this node.

I soon discovered that when a client was streaming and the parent was killed via the init.d script, the child serving the active client remained running and required manual killing.

To fix this problem I kept track of the children in the code and provided a clean-up routine when the script received a TERM signal. Below I list the $SIG{"TERM"} code to catch the TERM signal and 2 extra lines inside the main while loop to track the children as marked between the # >> tags:

# somewhere before the socket is used my %kids; $SIG{"TERM"} = "cleanup_and_exit"; sub cleanup_and_exit { my $sig = @_; foreach my $kid (keys %kids) { #reap them warn("Failed to reap child pid: $kid\n") unless kill + 9, $kid; } # it's a good idea to exit when we are told to exit(0); } # -- bits left out here -- #wait for connections at the accept call while (my $connection = $listen_socket->accept) { my $child; # perform the fork or exit die "Can't fork: $!" unless defined ($child = fork()); if ($child == 0) { #i'm the child! #close the child's listen socket, we dont need it. $listen_socket->close; #call the main child rountine play_songs($connection); #if the child returns, then just exit; # >> undef $kids{$child}; # >> exit 0; } else { #i'm the parent! # >> $kids{$child} = 1; # >> #who connected? warn "Connecton recieved ... ",$connection->peerhost,"\n"; #close the connection, the parent has already passed # it off to a child. $connection->close(); } #go back and listen for the next connection! } # -- bits left out here --
I'm not a great Perl programmer, but this works flawlessly on my Debian system and has provided my an exceptional motivational tool to start learning socket programming. I hope I havne't overlooked too much error checking or done anything dangerous with my reaping and this can be of some use to someone.

Thanks again.

In reply to Re: MP3 server with IO::Socket by ryan
in thread MP3 server with IO::Socket by perlmonkey

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2020-05-27 10:38 GMT
    Find Nodes?
      Voting Booth?
      If programming languages were movie genres, Perl would be:

      Results (154 votes). Check out past polls.