Question

OS: Linux(RedHat) Programming Language: C++

I need to create a daemon(process) for Linux using C++ that will continuously listen on a custom port for PHP requests. The PHP will send the request data in XML form to the daemon, the daemon will parse the XML using Xerces and send back an appropriate reply in XML form to the PHP page.

I have successfully created a daemon process listening on port 4646 on localhost, but what I can't figure out is how the request from PHP will go to the daemon and how will the daemon send the reply back.

I tried google-ing for this particular problem but couldn't find a solution at all. Any kind of help on this problem will be very much appreciated.

I have also read a little about PHP daemons, but I'm not sure whether they are applicable in this particular scenario.

This approach is not hard and fast so any alternative approach will also do. The only thing hard and fast is the results i.e succesful communication between the PHP pages and the daemon.

Was it helpful?

Solution

Its better that u can use the php socket library to connect with the daemon running in your system and then u can pass data to the daemon and can process the result sent back by the daemon .

You can refer the PHP Socket Library for creating code to do socket connection with daemon ...

I think this is a better option than using CURL as the daemon is also a custom socket interface , CURL will be most suitable for HTTP request's , but i think here the daemon is not an HTTP one..

OTHER TIPS

Question is rather confused.

I need to create a daemon(process) for Linux using C/C++

Why does it have to be written in C or C++?

I have also read a little about PHP daemons, but I'm not sure whether they are applicable

Does that mean it doesn't need to be written in C/++? Why do you think they might not be applicable?

the daemon will parse the XML using Xerces

Why does it have to use Xerces? Presumably the daemon is supposed to do something more than just parse XML and compose a response - what else does it do?

Writing a daemon is not a trivial process. Writing a socket server is not a trivial process. It is somewhat simplified by implementing a well defined protocol at each end.

...which rather begs the question, why not just use HTTP as the protocol and a webserver to implement the server stuff, and seperate the application-specific logic into a [f]CGI program. And taking this one step further, why not implement the application-specific logic using PHP.

The only thing hard and fast is the results i.e succesful communication between the PHP pages and the daemon

Some options:

  1. Write the application specific part as a PHP page then invoke it via an HTTP request using curl

  2. Write the server as a single tasking stdio server and use [x]inetd to invoke it, handling the client side connection as a network socket (requires that you define your protocol)

  3. Write a forking server daemon in PHP handling the connection at both ends as a network socket (requires that you define your protocol)

  4. write a single threaded server daemon (using socket_select) in PHP handling the connection at both ends as a network socket (requires that you define your protocol)

Of course anywhere I've mentioned PHP above, you could equally use C, C++, Perl, Java....etc.

xinetd / inetd might be a bit old skool but can make this easy and scalable (with in limits)

Inetd will call you program and send the the traffic to stdin and your stdout will go to the connection. As long as you dont need shared information it stops you having to worry about making the program bug free/no memory leaks etc....

Simon Loader

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top