Page 1 of 1

Client disconnection

Posted: Mon Jan 04, 2010 9:59 am
by nocia
Hi all,

I use a PHP server (P) with which I'd like to do XML over HTTP communication with an other server running J2EE (J).

The communication protocol is the following :
J sends a post request to P. P responds with some "echo" to J (OK or NOK for a xml problem for example). If the response is transmitted to J, P process this request. Otherwise P cancels the request and J will resend its request.
So the HTTP request and response are done in the same TCP session.

My problem is that even if the connection is down and P echo the response, P doesn't see that the connection is down and so can't cancel the process to wait the next request from J.
I test to detect that with the connection_aborted function in the register_shutdown_function. However even with flushing the echo this solution doesn't seem to work.

Thus I'd like to know if there is a way to know if the client is still connected when I echo something from the PHP server.

I thought to get the output stream as one can do in Java but I don't find anything to carry out this.

Thanks a lot in advance for any help :)

Nocia

Re: Client disconnection

Posted: Mon Jan 04, 2010 2:55 pm
by requinix
nocia wrote:I test to detect that with the connection_aborted function in the register_shutdown_function.
...If you wait until shutdown to decide whether your script should continue running, it's too late. You need to check while it's still processing.

Re: Client disconnection

Posted: Mon Jan 04, 2010 3:28 pm
by jason
Are you running a server written in PHP, or are you running PHP on top of something like Apache? If you aren't playing around with sockets on the PHP end, you are doing it wrong. =)

Remember, PHP isn't like Java, and works in a different way.

Re: Client disconnection

Posted: Wed Jan 06, 2010 1:57 am
by nocia
tasairis wrote:
nocia wrote:I test to detect that with the connection_aborted function in the register_shutdown_function.
...If you wait until shutdown to decide whether your script should continue running, it's too late. You need to check while it's still processing.
OK but if I do this test inside the script, connection_aborted always returns me 0...
To test that I use a script like that :

Code: Select all

 
echo "before";
flush();
sleep(10);
echo "after";
flush();
write_in_a_file(connection_aborted());
 
And I break the connection during the sleep.

Re: Client disconnection

Posted: Wed Jan 06, 2010 2:12 am
by nocia
jason wrote:Are you running a server written in PHP, or are you running PHP on top of something like Apache? If you aren't playing around with sockets on the PHP end, you are doing it wrong. =)

Remember, PHP isn't like Java, and works in a different way.
Yes I run PHP on Apache.
So do you mean it isn't possible to know if the connection is down without using directly the socket?
I didn't use sockets in order to not have to process manually the HTTP arguments...

Yes I'm clearly not a PHP expert :) . But I thought that when I do an echo this will write in a buffer handled by Apache. And if I flush this buffer Apache will send it to the client and so see whether the connection is down. An when I call connection_aborted() I will get the connection state given by Apache.
Is it wrong?

And what do you thing to be the best PHP way to handle such a problem?

Thanks a lot for your help.