简体   繁体   English

在Linux中的C可执行文件和PHP之间传递数据结构?

[英]Passing data structures between C executable and PHP in Linux?

in my previous question: Passing data between PHP and C executable in linux i was able to pass strings with 在我之前的问题中: 在Linux中的PHP和C可执行文件之间传递数据时,我能够使用

proc_open();

in PHP and 在PHP和

fget(stdin,"r");

in C ; 在C; after that, i used strtok(); 之后,我使用了strtok(); to split up my string and turn them into values in C 拆分我的字符串并将其转换为C中的值

is there any other ways to pass data structure? 还有其他方法可以传递数据结构吗? since my way doesn't seems to be a good idea 因为我的方法似乎不是一个好主意

On unix and therefore linux, text is the universal interface. 在UNIX上,因此在Linux上,文本是通用接口。 You can pass text via stdin/stdout and parameters to scripts and binary programs, and also convey information with environmental variables. 您可以通过stdin / stdout和参数将文本传递给脚本和二进制程序,还可以传递带有环境变量的信息。 All you have to do is parse the text. 您所要做的就是解析文本。 But Data Structures like hash-maps,linked lists and tree sets,etc can not be passed between distinct processes. 但是无法在不同的进程之间传递哈希结构图,链接列表和树集等数据结构。

So using this methods are classics on command like scripts and binary programs, after that there is using file locking to read text file databases which is messy to say the least. 因此,使用此方法是脚本和二进制程序等命令的经典之作,此后至少可以使用文件锁定读取文本文件数据库。

And then is the realm of binary data passing between processes, which MUST be encoded in some kind of common protocol. 然后是进程之间传递的二进制数据的领域,必须以某种通用协议对其进行编码。 Here you start playing with file pipes, unix sockets and network sockets. 在这里,您开始使用文件管道,Unix套接字和网络套接字。

I won't get into details of each, network sockets even the local loopback device is very favored today for universal client/server style of communication. 对于通用的客户端/服务器风格的通讯,即使本地环回设备今天也非常受青睐,我将不逐一介绍每个网络插座的细节。 because allows multiplexing of communications and is easily portable. 因为允许通信的多路复用并且易于携带。

I won't give you code, you have plenty of data to google now and code for every single kind of communication would be way too big. 我不会给您代码,您现在有很多数据要向Google发送,并且每种通讯的代码都太大了。

Here are some tutorials on sockets: 这是一些关于套接字的教程:

www.linuxhowtos.org/C_C++/socket.htm www.linuxhowtos.org/C_C++/socket.htm
http://gnosis.cx/publish/programming/sockets.html http://gnosis.cx/publish/programming/sockets.html

AS one last caveat. 作为最后的警告。 Sockets are not as simple as stdin/stdout data passing, so your needs really must be complex enough to justify the use of sockets data transmision. 套接字不像stdin / stdout数据传递那样简单,因此您的需求确实必须足够复杂,以证明使用套接字数据传输是合理的。

stdin/stdout is practical for most wrapping situation, specially for glue code. stdin / stdout适用于大多数包装情况,尤其是胶水代码。

Edit: there is also a "third option", using a database like mysql or postgres, where the sockets are wrapped by the database api. 编辑:还有一个“第三种选择”,使用像mysql或postgres这样的数据库,其中套接字由数据库api包装。 An the API and bindings for many popular languages as PHP, C, java, perl, python, ruby, etc, allow inter process data exchange in a ordered and safe way. API和许多流行语言(例如PHP,C,java,perl,python,ruby等)的绑定允许以有序且安全的方式进行进程间数据交换。 But then you have to learn a database api, sql, maybe some normalization and best practices... Your experience and available features will be greater but not necessarily quicker or easier. 但是,然后您必须学习数据库api,sql,也许还需要一些规范化和最佳实践……您的经验和可用功能将更好,但不一定会更快或更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM