gpt4 book ai didi

php - Socket发送终端输入的数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:11 25 4
gpt4 key购买 nike

我想创建一个套接字来监听连接并将我在终端中输入的数据(想使用 php script.php 运行它)发送给所有客户端

有什么方向看吗?

最好的问候, jack

最佳答案

这个脚本做你想做的,也将客户端发送的数据写入你的终端,使用 php script.php 运行:

<?php

$port = 12345;
$server = stream_socket_server("tcp://localhost:$port", $errno, $errstr);
if (!$server) die("$errstr (errno $errno)\n");

$s = array(STDIN, $server); // initially wait for terminal input or connections
while ($r = $s and stream_select($r, $n=NULL, $n=NULL, NULL))
foreach ($r as $stream)
if ($stream == STDIN) // terminal input
{
$data = fgets(STDIN);
foreach (array_slice($s, 2) as $client) // clients from index 2 on
fputs($client, $data);
}
else
if ($stream == $server) // new client
$s[] = stream_socket_accept($server, -1); // add it to $s (index 2 on)
else // data from a client
if (!fputs(STDOUT, fgets($stream)))
{
fclose($stream);
array_splice($s, array_search($stream, $s), 1); // remove client
}

?>

关于php - Socket发送终端输入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38054908/

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