Link to home
Start Free TrialLog in
Avatar of benmanning
benmanning

asked on

C++ Daemon & PHP Communication

Basically I have written a daemon in C++ that needs to communicate over HTTP (probably via PHP).

Now I'm not sure of the best way to do this, but I have thought about using TCP: The PHP script would create a TCP connection to the daemon and perform the communication and format/return the result.  I have also thought linux pipes would do the same thing if supported by PHP (or maybe I could write a PHP module to implement them).

It would be nice if the PHP script could call the daemon directly.  Maybe I could save a function pointer in a file for PHP to read?  Surely there would be seg fault if PHP tried to access the daemon's memory from a different process?

I would be interested to hear from anyone who may have done this before or have and ideas on the best method to use.

cheers,
Ben

The system is running on Gentoo Linux
Avatar of mespinozae
mespinozae
Flag of Costa Rica image

Hello :)

I havent done this exactly but think you can use sockets for this, they are supported by php and should probably help.... additionally a php script can run any external programs with the exec function:
http://www.php.net/function.exec

And the exec returns any output from the program you run so you can probably use this for communicating both parts...
Avatar of evilrix
WHy do you want to do this via PHP? Why not directly from your C++ code? Take a loot at LibCurl, which is a C/C++ library that provides, amongst other things, support for HTTP.

http://curl.haxx.se/libcurl/

"libcurl is a free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos4), file transfer resume, http proxy tunneling and more!

libcurl is highly portable, it builds and works identically on numerous platforms, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows, Amiga, OS/2, BeOs, Mac OS X, Ultrix, QNX, OpenVMS, RISC OS, Novell NetWare, DOS and more...

libcurl is free, thread-safe, IPv6 compatible, feature rich, well supported, fast, thoroughly documented and is already used by many known, big and successful companies and numerous applications. "

Avatar of benmanning
benmanning

ASKER

evilrix,
I'm thinking PHP will save me some development of writing a webserver in the C++ daemon.  Remember everything I have described above will be based on a server in a datacantre.  Libcurl looks like a nice library but it only supports client requests?

I thought about using sockets as well but it seems a bit messy?  As evilrix highlighted, I could host a webserver from the C++ daemon if there is a library for it.  I thought there may be a way for stateless PHP code (or similar) to call a stateful C function!  PHP exec could work but how would the function it called communicate with the daemon?
>> Remember everything I have described above will be based on a server in a datacantre
Ah ok that makes a little more sense then :)

Sorry, that wasn't clear to me when I read the Q first time round.
I'm just wondering if you could do this using Apache and have your code run as CGI maybe?
If your daemon is listening on a port for incoming requests, then you can indeed use your webserver running PHP as a front-end. PHP is capable of working with sockets (as mespinozae mentioned) :

        http://www.php.net/sockets

so you can open a connection to the daemon, format whatever it returns, and send it back to the client.
Sorry, I should of described it more clearly.  Maybe it would be possible using CGI but I don't think it would be much different to PHP.  The main problem here is I'm not sure how to expose an API from a daemon...Is it even possible?

Again, I could do it using sockets but I thought that wouldn't be a clean solution?  Or would that be the best way?
Maybe you need to clarify what the daemon does, and what functionality you need to access in it ?
Here it is:
The daemon is similar to an SQL server (in that it holds lots of different info about schools).  A flex application running on some client PC somewhere in the world will make an HTTP request when it needs data.  Eg the request could be: get a list of students in this class.  The server will recieve the HTTP request (possibly via PHP or CGI?) and 'somehow' invoke some command on the daemon to process that request.  The 'somehow' is the bit i'm not sure of what the best method is!

The daemon also keeps track of the clients logged into the system, the login process and other process related stuff.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if the client has to have internet access why not using a standard database, like mysql? and use something like SqLite to fetch the data?
Infinity08:
How can PHP keep a connection open between requests?  I guess I would have to somehow store a socket variable?  Maybe I could have a class that pooled open connections so that a request got the next available open conection!
The daemon handles other stuff as well and provides data cache'ing wich would not be possible with just an SQL server.  Infact I've seen http://vochat.com/ which uses a C daemon.  Thier daemon appears to communicate using TCP!  ;)
>> How can PHP keep a connection open between requests?

You could use this :

        http://www.php.net/pfsockopen

I've used PHP several times before, but wouldn't call myself an expert - maybe one of the PHP experts can give some more insight into this ...


>> Infact I've seen http://vochat.com/ which uses a C daemon.

Yes, they seem to use a similar architecture to the one I described.
Do you know if the pfsockopen function would use the existing connection if another instance of that script was already running?  It would be nice if multiple scripts running at the same time all had thier own TCP connection to communicate over.  If there was a 'connection pool' they wouldn't need to re-open the connection each time, so there would be say 20 simultaneous connections.  The Daemon is multithreaded so could process many requests at once...
As far as I know, it opens a connection per process.
Sorry about the delay accepting this question.  I can now say that I used TCP for C++ ans PHP to communicate and it has worked like a dream!  It creates a new non-persistent connection for each request.  During peak periods it serves as many as 1 million requests per day and it handles it very well!  :D