沟通的C程序和PHP [英] communicate c program and php

查看:445
本文介绍了沟通的C程序和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个网页(用PHP编写的,因为这是我知道的),显示的输入值。我想这个值要传递给正在运行的是C programa。

I want to have a web page (written in php because it's what i know) that displays an input value. I want that value to be passed to a c programa that's already running.

我使用套接字两种工艺之间进行通信的,虽然,但我怎么能做到呢?我该如何使用的fsockopen连接到本地套接字。

I though of using sockets to communicate between both process, but how do I manage to do that? how can I use fsockopen to connect to a local socket.

推荐答案

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

Some simple solutions I can think of are:

您可以使用 Redis的作为使用的 hiredis 作为C客户端库。我从来没有使用hiredis库之前,但这样做只是现在您测试和库真的很好。我能知道它,因为Redis是C code的最好的一块,我知道。)

You could use redis as your ipc using hiredis as your c client library. I never used hiredis library before but did it just now for you to test and the library is really good. I could have known it because redis is the best piece of C code I know :).

修改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客户端库替换此,但将是非常容易的:

from another tab start start redis-cli(for prototyping) and issue the following command. You should replace this with predis as php client library, but that is going to be very easy:

lpush php "Hello from PHP"

内部运行hiredis,例如:

您应该看到消息Hello从PHP。易如反掌,如果你问我:)

You should see the message "Hello from PHP". Easy as pie if you ask me :).

您可以使用命名管道的。

您可以阅读 Beej的指南网络编程使用Internet套接字。在我看来这是一个非常良好的阅读。

You could read the Beej's Guide to Network Programming Using Internet Sockets. In my opinion this is a very good read.

这篇关于沟通的C程序和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆