Client disconnection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nocia
Forum Newbie
Posts: 3
Joined: Mon Jan 04, 2010 9:28 am

Client disconnection

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Client disconnection

Post 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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: Client disconnection

Post 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.
nocia
Forum Newbie
Posts: 3
Joined: Mon Jan 04, 2010 9:28 am

Re: Client disconnection

Post 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.
Last edited by nocia on Wed Jan 06, 2010 2:13 am, edited 1 time in total.
nocia
Forum Newbie
Posts: 3
Joined: Mon Jan 04, 2010 9:28 am

Re: Client disconnection

Post 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.
Post Reply