# 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 --