473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

PHP persistent UNIX sockets between sessions

Hello,

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
3 8816
pbmods
5,821 Expert 4TB
Heya, Chris. Welcome to TSDN!

Have a look at pfsockopen().
Jul 26 '07 #2
Heya, Chris. Welcome to TSDN!

Have a look at pfsockopen().
Hi,

Thanks!

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);
  4.  
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 http://www.xajaxproject.org/). 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

Chris
Jul 27 '07 #3
bucabay
18
I know this is an old thread.

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

stream_socket_client("unix:///tmp/hs.socket",$errno,$errstr,2.0,
STREAM_CLIENT_CONNECT|
STREAM_CLIENT_PERSISTENT);

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

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

Similar topics

5
by: Felix E. Klee | last post by:
I've set up a PHP web application where users can log in and open a connection to a NNTP-server. There is a *one-to-one* relationship between sessions and NNTP-connections (i.e. exactly one...
5
by: Virgil Green | last post by:
An associate has asked me to take a look a problem he's having with starting mysql. I thought I'd give it a shot to learn some more about mySQL before having him turn it over to someone more...
1
by: brad | last post by:
Hi, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the...
6
by: David Rasmussen | last post by:
I use urllib2 to do some simple HTTP communication with a web server. In one "session", I do maybe 10-15 requests. It seems that urllib2 opens op a connection every time I do a request. Can I...
15
by: Alpha | last post by:
I was told that Unix API can only be called using C++, ATL and MFC. However, I was also told that C# can do that through Pinvoke to a DLL that interfaces with the Unix API. Can someone direct me...
4
by: keegan.csmith | last post by:
Hi, I have started a new small web project, and was wondering if there are any good guides on how to do Persistent Sessions and Authentication using python and CGI. I don't really want too use...
2
by: howachen | last post by:
Hi, Are there something like a `Persistent global variable` in PHP? It is something like `Application` in the classic ASP, all the request share the same variable, and the die after a timeout.
0
by: mandarkraftware | last post by:
Hi, We have the following C++/Unix consultant with us immediately. Consultant Name : Khalid Ansari  Proven record of 7 years of total IT experience.Experience includes roles as Designer,...
3
by: sanchita | last post by:
Hello everyone, I didn't get any response in "Security" forum hence posting here again. I am having problem with persistent cookies. Even after setting "CreatePersistentCookie" to true in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.