简体   繁体   English

用PHP调用C程序

[英]Calling C program with PHP

I've written a small database engine in C that works by reading commands you input in the console and it outputs the result. 我在C中编写了一个小型数据库引擎,它通过读取您在控制台中输入的命令来工作,并输出结果。 Is there any way of me writting some PHP code that could send arguments to the console and recieve the output back to PHP without restarting the compiled program. 有没有办法让我编写一些PHP代码,可以将参数发送到控制台并将输出接收回PHP而无需重新启动已编译的程序。 Is there a better way of doing this? 有没有更好的方法呢?

You say you want the PHP to send and receive messages to your program without restarting the compiled program. 您说您希望PHP在不重新启动已编译程序的情况下向程序发送和接收消息。

So I don't think using shell_exec or proc_open will work how you want, since these commands both load a fresh instance of the compiled program. 所以我不认为使用shell_exec或proc_open会按你想要的方式工作,因为这些命令都加载了编译程序的新实例。

Instead, I suggest you look into sockets , and how you would rewrite your database engine to use those instead of STDIN/STDOUT. 相反,我建议您研究套接字 ,以及如何重写数据库引擎以使用它们而不是STDIN / STDOUT。 Then you can use PHP's socket functions to communicate between your applications. 然后,您可以使用PHP的套接字函数在您的应用程序之间进行通信。 And you'll have just one instance of your compiled program running in the background, even with multiple hits to your PHP script. 而且你只需要在后台运行一个已编译程序的实例,即使你的PHP脚本有多次点击也是如此。

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

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