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

Wise monks, I seek your knowledge and experience.

I have a non-parsed header CGI that runs in a loop once started. Once the user hits "Stop" or moves on to another page, I want to end the script and do cleanup.

Is there any way to check to see if the client is still connected?

ryddler

Replies are listed 'Best First'.
(tye)Re: Checking for NPH client connection
by tye (Sage) on Jan 05, 2001 at 01:24 UTC

    Um, depends. (: If your server does non-parsed headers by giving you a pipe to write to and then notices that the client has closed the socket, you might be able to detect the action that the server takes after noticing. If it closes its handle to the pipe, then your next write to the pipe will generate a SIGPIPE which you can catch. If the server kills you, then you can catch that.

    If the server does non-parsed headers by passing the socket directly to you as your STDOUT, then when the client closes the socket your next write to the socket will fail (print actually has a return value, even though it is usually ignored). If you aren't constantly writing to STDOUT, then you can use select() to detect when the socket gets closed by the client.

            - tye (but my friends call me "Tye")
Re: Checking for NPH client connection
by Hot Pastrami (Monk) on Jan 04, 2001 at 22:10 UTC
    I believe that $SIG{PIPE} will indicate if the browser has disconnected... perhaps something like this would work:
    $SIG{PIPE} = sub { exit; }
    ...or, if you have a sub already which performs the cleanup, perhaps called "cleanup()"...
    $SIG{PIPE} = \&cleanup;

    Hot Pastrami