gpt4 book ai didi

javascript - 我如何从 linux 的 stdout 读取数据到 ajax 并获得某个字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 02:56:41 26 4
gpt4 key购买 nike

我在 linux 中有一个 c 程序,它使用 fwrite 读取 php 发送的数据并在 linux 中输出一个字符串。

Ajax

$.ajax({
type: "POST",
data: {cmd:"IN"},
url: "COMMAND.php",
success: function(data){

//Get only this string ">" from the stdout of linux and store it to a variable.

}
});

命令.php

<?php

if($_POST['cmd']=="IN"){

$fd = fopen("/tmp/myFIFO","w");
fwrite(fd,"IN");
fclose(fd);

}


?>

C

char buf[255];
while((n = read(fd, buf, sizeof buf - 1)) > 0 ){ //fd is the opened pipe.

buf[n - 1] = '\0';

if(strcmp(buf,"IN")==0){
printf("%s\n", "FF0023sff344>fdfslldlf");
}

}

如何从 linux 捕获字符串“FF0023sff344>fdfslldlf”到 ajax 并只获取字符串“>”..?

最佳答案

您可能想要一个与上述不同的工作流程。如果你需要你的 C 进程保持运行,你应该为它打开一个双向套接字。

要做到这一点,你需要考虑让你的 C 程序成为某种类型的套接字服务器。查看 listenbindaccept,然后选择开始。

在 PHP 方面,查看函数的 sockets 集合。

或者,使用 http://libevent.org/ 之类的东西让你的 C 程序成为一个轻量级的 http 服务器会更容易一些。然后在 php 端使用 curl 向其中发出请求。

关于javascript - 我如何从 linux 的 stdout 读取数据到 ajax 并获得某个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16587802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com