dongzhuji1042 2011-11-28 10:54
浏览 101
已采纳

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

in my previous question: Passing data between PHP and C executable in linux i was able to pass strings with

proc_open();

in PHP and

fget(stdin,"r");

in C ; after that, i used strtok(); to split up my string and turn them into values in C

is there any other ways to pass data structure? since my way doesn't seems to be a good idea

  • 写回答

1条回答 默认 最新

  • dsv73806 2011-11-28 11:42
    关注

    On unix and therefore linux, text is the universal interface. You can pass text via stdin/stdout and parameters to scripts and binary programs, and also convey information with environmental variables. 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.

    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.

    Here are some tutorials on sockets:

    www.linuxhowtos.org/C_C++/socket.htm
    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 is practical for most wrapping situation, specially for glue code.

    Edit: there is also a "third option", using a database like mysql or postgres, where the sockets are wrapped by the database 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. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看