By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,351 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,351 IT Pros & Developers. It's quick & easy.

PHP persistent UNIX sockets between sessions

P: 2

I have an application which runs on a single linux box, with a C++ backend server and a web-based front end, with apache also running on the local server.

I'm using PHP to communicate with the C++ backend, and would like to just connect to the C++ server once, and have bilateral communication as follows:
1. As the user carries out actions on the web front end, just use fwrite to communicate with the backend on the persistent socket.
2. Have a small javascript routing using timeout to regularly call a php routine to process any (asynchronous) messages the server might send us on the socket. This is the primary reason I want the socket to stay connected.

I should point out that the volume of traffic is pretty low - this is a "control" application for some hardware, but we're talking a few kbytes an hour.

The problem I'm encountering is that I cannot find a way to pass a socket object between one call to a PHP script and another. The PHP sessions paradigm works great for passing integers and strings for example, but doesn't work for passing a socket object. I can understand why that would be the case, but does anyone have any hints as to how I might achieve what I'm trying to do?

I like PHP and I am very familiar with sockets, so that's why I'm trying to do it this way, but I'm not particularly wedded to either if someone has a suggestion for a better approach. I *DO* need to use a browser front end for other reasons.

Any help much appreciated.

Chris Hegarty
Jul 26 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 5,821
Heya, Chris. Welcome to TSDN!

Have a look at pfsockopen().
Jul 26 '07 #2

P: 2
Heya, Chris. Welcome to TSDN!

Have a look at pfsockopen().


Actually I am already using persistent sockets, the actual call I'm using is:
Expand|Select|Wrap|Line Numbers
  1. stream_socket_client("unix:///tmp/hs.socket",$errno,$errstr,2.0,
  2.                            STREAM_CLIENT_CONNECT|
  3.                            STREAM_CLIENT_PERSISTENT);
Maybe I need to explain a bit what I'm doing - it may well be that I have a conceptual problem in my code which someone can identify.

I call the PHP script from javascript using xajax calls (see I only have a single PHP script with all of the functions I call from javascript in it, but obviously each call to a PHP function like the following:

Expand|Select|Wrap|Line Numbers
  1. function InitialSetup()
  2. {
  3. ....
  4. xajax_OpenSocket();
  5. ....
  6. }
runs the PHP script once, which then exits.

I have been trying to save the stream resource returned by stream_socket_client() in the $_SESSION array (which works just great for atomics like integers and strings ) but it does not seem to be able to save the PHP stream resource. So when I call the PHP script again, I no longer have the stream resource to access the socket (in other words, I assume it is still open, I just don't have any way to get to it). I also tried passing the PHP stream resource reference back up to javascript using xajax but that didn't work either (or at least I couldn't find a way).

Does anyone have a simple solution for this? I must be missing something obvious...

Thanks again

Jul 27 '07 #3

P: 18
I know this is an old thread.

What you need to do is forget about saving the variable returned from:


The stream_socket_client() is persisted when you make an identical call, from the same process.

So you can just use it normally, knowing that if the stream_socket_client() already has an equivalent socket resource, it will return that resource instead of creating a new one.

What to watch though is that you may not land on the same php process for each page load, and thus stream_socket_client() will not return the socket resource you created in the last page load.
May 12 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.