in reply to Re: Win32::Daemon + socket->accept: service cannot be stopped
in thread Win32::Daemon + socket->accept: service cannot be stopped [SOLVED]

Hi All,

Thanks for the help and ideas! It works with a fork() and kill() to kill the child.
I'll just paste the code that I changed:
... elsif( SERVICE_RUNNING == $State ) { our $sock = new IO::Socket::INET(LocalPort => MYPORT, Reuse => 1, Listen => 5 ) or LOGDIE "can't create local socket: $@\n"; INFO "Accepting connections on Port ", MYPORT, "..."; our $client; $SIG{'CHLD'} = sub { wait(); }; # No Zombies my $pid = fork(); if ($pid == 0) { # Child while ( 1 == 1) { if ( $client = $sock->accept() ) { INFO "Connection from ", $client->peerhost(), ":", $cl +ient->peerport(); while (<$client>) { unless ($_ =~ /^[\w.-_\s]+$/) { print $client "Invalid Input!. Type [h]elp for + help\n"; next; } chomp; $_ =~ s/\r$//; DEBUG "Got command: $_"; my $output = get_output($_); print $client $output; } INFO "Client " . $client->peerhost() . " has disconnec +ted."; } } } else { while ( SERVICE_STOP_PENDING != Win32::Daemon::State() ) { sleep 5; } #### We are done so close down... ### Win32::Daemon::State( SERVICE_STOPPED ); Win32::Daemon::StopService(); INFO "Need to go, bye"; kill(9, $pid); exit; } }
Thanks again,
Sven

Replies are listed 'Best First'.
Re^3: Win32::Daemon + socket->accept: service cannot be stopped
by Jenda (Abbot) on Jun 24, 2005 at 14:18 UTC

    I think it would be better to connect to the listening socket and issue a specific command than to kill the working thread. You might kill it at a wrong time.

    If you want to be safe you may for example generate a random number before the fork() and then include the number in the stop command. If you then accept stop requests only from localhost and only if they contain the right number you should be fairly safe.

    Jenda
    XML sucks. Badly. SOAP on the other hand is the most powerfull vacuum pump ever invented.

      Hi Jenda,

      you are probably right and if expecting many clients, I would certainly go down that road.
      As I only have one connection at a time currently, I'm just too lazy to implement that right now. I might try it further.

      Thanks again, Sven