作者:杨建谦461128 | 来源:互联网 | 2023-05-17 12:02
OS:Linux(RedHat)ProgrammingLanguage:C++Linux(RedHat)编程语言:c++。Ineedtocreateadaemon(proces
OS: Linux(RedHat) Programming Language: C++
Linux(RedHat)编程语言: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.
我需要使用c++为Linux创建一个守护进程(进程),它将持续监听PHP请求的自定义端口。PHP将以XML格式将请求数据发送给守护进程,守护进程将使用Xerces解析XML,并向PHP页面发送适当的XML格式答复。
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.
我已经成功地创建了一个监听localhost端口4646的守护进程,但是我不能确定的是来自PHP的请求将如何发送到守护进程,以及守护进程将如何发送回应答。
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.
我还阅读了一些关于PHP守护进程的文章,但我不确定它们是否适用于这个特定的场景。
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.
这种方法并不难也不快,所以任何其他的方法也可以。唯一困难和快速的是结果i。PHP页面和守护进程之间的成功通信。
3 个解决方案
2
Question is rather confused.
问题是,而困惑。
I need to create a daemon(process) for Linux using C/C++
我需要使用C/ c++为Linux创建一个守护进程(进程)
Why does it have to be written in C or C++?
为什么一定要用C或c++写?
I have also read a little about PHP daemons, but I'm not sure whether they are applicable
我也读过一些关于PHP守护进程的文章,但是我不确定它们是否适用
Does that mean it doesn't need to be written in C/++? Why do you think they might not be applicable?
这是否意味着它不需要用C/+来写?为什么你认为它们可能不适用?
the daemon will parse the XML using Xerces
守护进程将使用Xerces解析XML
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?
为什么要使用Xerces?想必守护进程应该做的不仅仅是解析XML和组成响应——它还做什么呢?
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.
…这就引出了一个问题,为什么不直接使用HTTP作为协议和webserver来实现服务器,并将特定于应用程序的逻辑分离到一个[f]CGI程序中呢?更进一步,为什么不使用PHP实现特定于应用程序的逻辑呢?
The only thing hard and fast is the results i.e succesful communication between the PHP pages and the daemon
唯一困难和快速的是结果i。PHP页面和守护进程之间的成功通信
Some options:
一些选项:
Write the application specific part as a PHP page then invoke it via an HTTP request using curl
将应用程序特定的部分作为PHP页面编写,然后使用curl通过HTTP请求调用它
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)
将服务器编写为单个任务stdio服务器,并使用[x]inetd来调用它,将客户端连接作为网络套接字处理(要求您定义您的协议)
Write a forking server daemon in PHP handling the connection at both ends as a network socket (requires that you define your protocol)
用PHP编写一个分叉服务器守护进程,以网络套接字的形式处理两端的连接(需要定义协议)
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)
在PHP中编写一个单线程服务器守护进程(使用socket_select),以网络套接字的形式处理两端的连接(需要定义协议)
Of course anywhere I've mentioned PHP above, you could equally use C, C++, Perl, Java....etc.
当然任何地方我上面提到的PHP,您同样可以使用C、c++、Perl、Java ....等等。