热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在c++中的Linux守护进程来处理PHP请求-LinuxDaemoninC++toprocessPHPrequests

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 个解决方案

#1


1  

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 .

最好使用php套接字库连接系统中运行的守护进程,然后将数据传递给守护进程,并处理守护进程发回的结果。

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

您可以参考PHP套接字库来创建与守护进程进行套接字连接的代码……

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..

我认为这是一个比使用CURL更好的选择,因为守护进程也是一个定制的socket接口,CURL将最适合HTTP请求,但是我认为这里的守护进程不是HTTP请求。

#2


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:

一些选项:

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

    将应用程序特定的部分作为PHP页面编写,然后使用curl通过HTTP请求调用它

  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)

    将服务器编写为单个任务stdio服务器,并使用[x]inetd来调用它,将客户端连接作为网络套接字处理(要求您定义您的协议)

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

    用PHP编写一个分叉服务器守护进程,以网络套接字的形式处理两端的连接(需要定义协议)

  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)

    在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 ....等等。

#3


0  

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

xinetd / inetd可能有点老了,但是可以使它更容易扩展(具有限制)

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....

Inetd将调用您的程序并将流量发送到stdin,您的stdout将进入连接。只要你不需要共享信息停止你不必担心程序缺陷/不自由内存泄漏等....

Simon Loader

西蒙装载机


推荐阅读
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 本文探讨了C语言中指针的应用与价值,指针在C语言中具有灵活性和可变性,通过指针可以操作系统内存和控制外部I/O端口。文章介绍了指针变量和指针的指向变量的含义和用法,以及判断变量数据类型和指向变量或成员变量的类型的方法。还讨论了指针访问数组元素和下标法数组元素的等价关系,以及指针作为函数参数可以改变主调函数变量的值的特点。此外,文章还提到了指针在动态存储分配、链表创建和相关操作中的应用,以及类成员指针与外部变量的区分方法。通过本文的阐述,读者可以更好地理解和应用C语言中的指针。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 微软评估和规划(MAP)的工具包介绍及应用实验手册
    本文介绍了微软评估和规划(MAP)的工具包,该工具包是一个无代理工具,旨在简化和精简通过网络范围内的自动发现和评估IT基础设施在多个方案规划进程。工具包支持库存和使用用于SQL Server和Windows Server迁移评估,以及评估服务器的信息最广泛使用微软的技术。此外,工具包还提供了服务器虚拟化方案,以帮助识别未被充分利用的资源和硬件需要成功巩固服务器使用微软的Hyper - V技术规格。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 本文介绍了5个基本Linux命令行工具的现代化替代品,包括du、top和ncdu。这些替代品在功能上进行了改进,提高了可用性,并且适用于现代化系统。其中,ncdu是du的替代品,它提供了与du类似的结果,但在一个基于curses的交互式界面中,重点关注占用磁盘空间较多的目录。 ... [详细]
author-avatar
杨建谦461128
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有