Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP套接字和进程打开_Php_Sockets - Fatal编程技术网

PHP套接字和进程打开

PHP套接字和进程打开,php,sockets,Php,Sockets,我正在尝试用PHP为Windows创建一个类似inetd的服务,以便将来与我的其他应用程序一起使用 所以我所能想到的就是使用Steam Server和proc_open将流直接输送到流程(如inetd)。因为在Windows上没有pcntl_fork(),PHP也不支持线程 到目前为止,这是我的代码。inetdtest程序是一个简单的程序,带有单个printf(用C编写)。但问题是,当我连接到服务器(通过netcat)时,没有收到响应消息 <?php define ('SERVICE_CO

我正在尝试用PHP为Windows创建一个类似inetd的服务,以便将来与我的其他应用程序一起使用

所以我所能想到的就是使用Steam Server和proc_open将流直接输送到流程(如inetd)。因为在Windows上没有pcntl_fork(),PHP也不支持线程

到目前为止,这是我的代码。
inetdtest
程序是一个简单的程序,带有单个
printf
(用C编写)。但问题是,当我连接到服务器(通过netcat)时,没有收到响应消息

<?php
define ('SERVICE_COMMAND', 'inetdtest');
define ('SERVICE_PORT', 35123);

function main() {
    echo "Simple inetd starting...\n";
    $socket = stream_socket_server('tcp://0.0.0.0:' . SERVICE_PORT, $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN);


    if ($socket === false) {
        echo "Can't bind to service port.\n";
        echo "[$errno] $errstr";
        die(1);
    }
    $processes = array();
    while (true) {
        $current = @stream_socket_accept($socket, 5, $host);
        if ($current !== false) {
            echo 'Incomming connection from client ' . $host . "\n";
            echo "Lunching child process... ";

            $io = array(
                0 => $current,
                1 => $current,
                2 => array('file', 'stderr.log', 'a')
            );

            $proc = proc_open(SERVICE_COMMAND, $io, $pipes, NULL, NULL, array('bypass_shell'));
            $status = proc_get_status($proc);
            echo " DONE! PID : {$status['pid']}\n";
            $processes[] = array($current, $proc);
        }
        foreach ($processes as $k=>$v) {
            $status = proc_get_status($v[1]);
            if (false === $status['running']) {
                echo "Finalizing process {$status['pid']}... ";
                fflush($v[0]);
                fclose($v[0]);
                proc_close($v[1]);
                unset($processes[$k]);
                echo "DONE!\n";
            }
        }
    }
}
main();

代码只是在这里工作(在linux上使用
cat
作为程序),所以问题就出在windows方面

首先,您要传递的绕过shell的选项应该如下所示

array('bypass_shell'=>true)
这可能已经解决了问题。这些事情的棘手之处在于,您正在将套接字fd传递给一个进程,这个进程可能会也可能不会正确处理它。我不知道这些事情在windows中是如何完成的,但将
cmd
从等式中剔除只会有所帮助

如果仍然不起作用,您应该创建一个循环,等待数据(来自网络或子进程)并将数据从网络套接字发送到进程管道,反之亦然