How to make a PHP Socket client crash-proof?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I have a Socket Client that connects to a Server. (It is not multi-socket or multi-anything.) It is VERY simple. How it works:

0. CLIENT connects to SERVER.
1. CLIENT waits for incoming data.
2. If there is no data for a while, CLIENT sends a "ping" to the SERVER.
3. If the SERVER responds, CLIENT continues waiting.
4. If the SERVER doesn't respond (and a few seconds pass), CLIENT closes socket and restarts (Go to Step 0.)
5. Once in a while, CLIENT must send some data to the SERVER
6. If the SERVER acknowledges the data, CLIENT begins waiting again for data from the SERVER (Go to Step 1.)
7. If the SERVER doesn't acknowledge, CLIENT closes socket and restarts (Go to Step 0.)


In a perfect world, the above script would run forever without trouble. Unfortunately, the world is not perfect.
Sometimes, the Server "dies" or "disappears" unexpectedly, at which point my Client hangs and starts consuming 100% CPU. (The problem.)


As I understand it, I must change the Client in a couple ways.

1. Set the Client's socket to NON-BLOCKING, so that it doesn't hang the script when the Server up and vanishes.
2. Use SOCKET_SELECT().


Now, I've read the docs and several good tutorials code examples, such as:

http://us2.php.net/manual/en/function.socket-select.php
http://us2.php.net/manual/en/function.socket-set-nonblock.php
http://dave.dapond.com/socketselect.php.txt
http://labs.jed.bz/HCJ/load.php.txt
http://www.amk.ca/python/howto/sockets/ sockets.html#SECTION000510000000000000000
http://www.catalyst.com/support/tutorials/tcpintro/page9.html


Unfortunately, all examples appear to differ from my Client in very one important way: They do not contain lines 5-7. None of them have to periodically stop listening for incoming data and check if they need to write to the socket (like mine does).

My Client MUST periodically check a database for new messages waiting to be sent. If there are any, it must write them to the Server. In my old code (which hangs when the Server disappears), I simply set the socket to blocking and give it a timeout of a couple seconds. So, every two seconds the Client times out, checks if there are any messages to send to the Server, sends them if there are, and then returns to waiting

If it helps, I can post snippets of my code, before (working, but not crash-proof) and after (not working at all, just looping endlessly, really fast).

Many thanks in advance for any suggestions you can offer.

...René

---
René Fournier
www.renefournier.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux