【问题标题】:communicate c program and php沟通c程序和php
【发布时间】:2011-06-14 22:53:00
【问题描述】:

我想要一个显示输入值的网页(用 php 编写,因为这是我所知道的)。我希望将该值传递给已经运行的 c 程序。

我虽然使用套接字在两个进程之间进行通信,但我该如何做到这一点?如何使用 fsockopen 连接到本地套接字。

【问题讨论】:

  • "如何使用 fsockopen 连接到本地套接字。" --- 与您对远程设备执行此操作的方式完全相同。
  • 是的,但是当你连接到远程的时候,格式会变成这样 fsockopen ("www.example.com", 80, $errno, $errstr, 30)。本地时的格式是什么?
  • Thks bensiu,所以,当您在本地计算机上打开套接字时,它们的 id 是端口对吗?所以如果我想在两个进程之间进行通信,我必须在端口上打开一个套接字,比如说 1025

标签: php c communication exec


【解决方案1】:

本地套接字只是一个文件,您可以像往常一样在其上使用fopen()、fwrite() 和fclose()。唯一的区别是您正在读取(或写入)另一个进程,而不是某个磁盘上的文件。

【讨论】:

    【解决方案2】:

    您可以使用命名管道。

    1. 一旦外部程序开始运行(或在此之前),请确保命名管道存在。见mkfifo。
    2. 打开管道以在外部程序中读取(就像打开常规文件一样)。
    3. 开始读取管道。默认情况下,程序会一直阻塞,直到有数据要读取。
    4. 打开用于写入 PHP 脚本的管道(同样,就像打开常规文件一样)。
    5. 将数据写入管道。外部程序现在将获取此数据。

    【讨论】:

      【解决方案3】:

      我能想到的一些简单的解决方案是:

      Redis

      您可以使用redis 作为您的ipc,使用hiredis 作为您的c 客户端库。我以前从未使用过hiredis库,但现在才为您测试,该库非常好。我本可以知道的,因为 redis 是我所知道的最好的 C 代码 :)。

      修改example.c

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include "hiredis.h"
      
      int main(void) {
          redisContext *c;
          redisReply *reply;
      
          c = redisConnect((char*)"127.0.0.1", 6379);
          if (c->err) {
              printf("Connection error: %s\n", c->errstr);
              redisFree(c);
              exit(1);
          }
      
          /* Blocking pop. */
          reply = redisCommand(c, "BLPOP php 0");
          if (reply->type == REDIS_REPLY_ARRAY) {
              if (reply->elements == 2) {
                  printf("%s\n", reply->element[1]->str);
              }
          }
          freeReplyObject(reply);
          redisFree(c);
          return 0;
      }
      

      编译运行示例:

      make
      ./hiredis-example
      

      来自 ./redis-cli:

      从另一个选项卡开始启动 redis-cli(用于原型设计)并发出以下命令。您应该将其替换为 predis 作为 php 客户端库,但这将非常简单:

      lpush php "Hello from PHP"
      

      内部运行hiredis-example:

      您应该会看到消息“Hello from PHP”。如果你问我,这很容易:)。

      命名管道

      您可以使用named pipes。

      套接字

      您可以阅读Beej's Guide to Network Programming Using Internet Sockets。在我看来,这是一本非常好的读物。

      【讨论】:

      • Redis 怎么样?提货快吗?
      • p.s:我自己从未在 C 中做过,但我现在正在尝试,编译示例开箱即用。现在我尝试让它使用 brpop...
      • @gvalero87。从 C 中阻止 pop 真的很容易。这个hiredis 库真的很棒:)。我将为您更新我的答案:P
      • 多么有趣的解决方案。这比自己使用套接字要好得多。很简单。很棒的东西。
      【解决方案4】:

      这是一个工作示例,其中 php 脚本向 C 守护程序发送请求,然后等待响应。它在数据报模式下使用 Unix 域套接字,因此速度很快。

      client.php

      <?php
      
      $file = "/tmp/myclient.sock";
      unlink($file);
      
      $socket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
      
      if (socket_bind($socket, $file) === false) {
        echo "bind failed";
      }
      
      socket_sendto($socket, "Hello world!", 12, 0, "/tmp/myserver.sock", 0);
      echo "sent\n";
      
      if (socket_recvfrom($socket, $buf, 64 * 1024, 0, $source) === false) {
        echo "recv_from failed";
      }
      echo "received: [" . $buf . "]   from: [" . $source . "]\n";
      
      ?>
      

      服务器.c

      #include <stdio.h>
      #include <sys/un.h>
      #include <sys/socket.h>
      
      #define SOCKET_FILE "/tmp/myserver.sock"
      #define BUF_SIZE    64 * 1024
      
      int main() {
        struct sockaddr_un server_address = {AF_UNIX, SOCKET_FILE};
      
        int sock = socket(AF_UNIX, SOCK_DGRAM, 0);
        if (sock <= 0) {
            perror("socket creation failed");
            return 1;
        }
      
        unlink(SOCKET_FILE);
      
        if (bind(sock, (const struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
            perror("bind failed");
            close(sock);
            return 1;
        }
      
        for (;;) {
          struct sockaddr_un client_address;
          int i, numBytes, len = sizeof(struct sockaddr_un);
          char buf[BUF_SIZE];
      
          numBytes = recvfrom(sock, buf, BUF_SIZE, 0, (struct sockaddr *) &client_address, &len);
          if (numBytes == -1) puts("recvfrom failed");
      
          printf("Server received %d bytes from %s\n", numBytes, client_address.sun_path);
      
          for (i = 0; i < numBytes; i++)
            buf[i] = toupper((unsigned char) buf[i]);
      
          if (sendto(sock, buf, numBytes, 0, (struct sockaddr *) &client_address, len) != numBytes)
            puts("sendto failed");
        }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-03
        • 2018-05-12
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        相关资源
        最近更新 更多