php - Apache 不想在 FIFO(管道)中 fopen 或 fwrite

标签 php c++ linux apache pipe

我在 .cpp 文件和 .html 文件之间建立正确的通信时遇到了问题。

所以我有三个文件:一个是 .cpp 服务器,创建管道并阻塞它直到消息到来,.php 文件打开管道并向其中写入一些内容,以及一个 .html 文件,其中只有一个按钮,运行脚本。

不寻常的是,当我运行我的服务器,然后从控制台运行 php 脚本时,一切正常,但如果我想通过浏览器和 html 页面执行此操作 - 管道无法打开。

Php 和 html 文件放置在/var/www/html/目录中,我的 cpp 文件位于我的主文件夹中。

我正在尝试在/tmp/文件夹中创建管道。

我尝试更改这两个路径的 chmod 和 chown 权限(并且我知道 apache = www-data),但目前无济于事。我 100% 没有安装 SELinux。

我真的非常感谢任何帮助,我已经尝试解决这个问题几个小时了......

服务器.cpp

#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std; 

const char MSG_LENGTH = 9; 
string FIFO_1 = "/tmp/fifo"; 

int main() {

    if ( mkfifo(FIFO_1.c_str(), S_IFIFO | 0666 ) == -1 ) {
        cout << "Cannot create fifo" << endl; 
        return 1; 
    }

    cout << "Fifo created" << endl;

    int readfd = open(FIFO_1.c_str(), O_RDONLY); 
    if ( readfd < 0 ) {
        cout << "Cannot open fifo" << endl; 
        return 1; 
    }

    char buffer[1024]; 
    if ( read(readfd, buffer, MSG_LENGTH) < 1 ) {
        cout << "Cannot read from fifo" << endl; 
        return 1; 
    }
    buffer[MSG_LENGTH] = '\0'; 

    cout << "Message: " << buffer << endl; 

    close(readfd); 
    unlink(FIFO_1.c_str()); 

    return 0; 
}

function.php

<?php

    $pipe_name = '/tmp/fifo'; 
    $msg = "message"; 

    $pipe = fopen($pipe_name, 'w'); 
    if ( $pipe == false) 
        echo "Cannot open fifo"; 

    if ( fwrite($pipe, $msg) == false ) 
        echo 'Cannot write to fifo';
    else 
        echo 'Can write to fifo'; 

?>

最佳答案

我正在运行的代码:

#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std; 

string FIFO_1 = "/blah/fifo"; 

int main() {

    if ( mkfifo(FIFO_1.c_str(), S_IFIFO | 0666 ) == -1 ) {
        cout << "Cannot create fifo" << endl; 
//        return 1; 
    }
    else
    {
    cout << "Fifo created" << endl;
    }

    int readfd = open(FIFO_1.c_str(), O_WRONLY); 
    if ( readfd < 0 ) {
        cout << "Cannot open fifo" << endl; 
        return 1; 
    }

    int count = 0;
    for(;;)
    {
    count++;
    cout << "Sending message " << count << endl;
    time_t t = time(NULL);
    string msg = ctime(&t);
    if ( write(readfd, msg.c_str(), msg.length()) < 1 ) {
        cout << "Cannot write to fifo" << endl; 
        return 1; 
    }
    sleep(1);
    }

    close(readfd); 
//    unlink(FIFO_1.c_str()); 

    return 0; 
}

以及 PHP 代码:

<?php

$pipe_name = '/blah/fifo'; 
$msg = "";

$pipe = fopen($pipe_name, 'r'); 
if ( $pipe == false) 
{
    echo "Cannot open fifo<br/>";
    $err = error_get_last();
    var_dump($err);
}
else
{   
    $count = 0;
    $data = stream_get_meta_data($pipe);
    var_dump($data);
    echo "<br/>";
    echo date("Y-M-d H:i:s") . "<br/>";
    echo "Waiting for message:";
    while(1)
    {
        $msg = fgets($pipe, 30);
        $err = error_get_last();
        if ($err)
        {
            echo "Err = "; var_dump($err); echo "<br/>";
        }
        else
        {
            echo "Got = " . $msg . "<br/>";
        }
        $count ++;
        if ($count > 20)
        {
            break;
        }
    }
}

echo "<br/>Done...";

?>

关于php - Apache 不想在 FIFO(管道)中 fopen 或 fwrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002946/

相关文章:

php - HTML 输入文本框异常行为

php - 通过Ajax调用PHP函数

php - 优化/简化多层分类查询

C++、ANTLR 和 VECTORS

Linux-查看Linux进程历史

linux - 使用 configure 创建一个插件,autogen make 就像在 gst 1.0 源代码中一样。也可以为树莓派 2 创建一个工具链

c - 如何知道我从哪里启动

php - 根据最后未更改的列数据从 mysql 获取行

c++ - 获取 OpenGL 纹理中特定像素的颜色?

c++ - 将其他库与静态库一起使用?