douzhanglu4591 2018-07-13 09:26
浏览 133

我想php继续监听socket(php socket编程)

I have a device which sends data in every 5 seconds using tcp and i am using php socket programming

and my code isn't working this code print nothing and when i use this code without loop(while loop) then it works i want to continue listen the data that my client sends in every 5 seconds please help me out....thanks in advance.

    <?php
error_reporting(E_ALL);
set_time_limit (0);

$host = '103.21.XX.XX';

$port = 60000;
$con = 1;
$word = "";

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die(socket_strerror(socket_last_error()));
$result = socket_bind($socket, $host, $port) or die(socket_strerror(socket_last_error($socket))); 

$result = socket_listen($socket) or die(socket_strerror(socket_last_error($socket)));

while ($con==1) {
    # code...

   $spawn = socket_accept($socket) or die("Could not accept incoming connection
");
    $input = socket_read($spawn, 2048) or die("Could not read input
");
     $input=(String)$input;
echo $input;
    if ($input == 'exit') 
    {
        $close = socket_close($sock);
        $con = 0;
    }

    if($con == 1)
    {
        $word .= $input;
    }
}

echo $word;

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog