ReactPHP侦听TCP端口

时间:2017-07-20 21:25:43

标签: php sockets tcp

我能够使用nc从服务器读取数据:

nc -vvl -p 1234
Listening on any address 1234 (search-agent)
Connection from 192.168.1.11:56929
24203   24204   24205   24206   24207   ^CExiting.
Total received bytes: 30
Total sent bytes: 0

我没有通过ReactPHP做同样的事情:

<?php
require __DIR__ . '/vendor/autoload.php';

use React\Socket\ConnectionInterface;

$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\TcpConnector($loop);

$connector->connect('1234')->then(function (ConnectionInterface $conn) use ($loop) {
    $conn->pipe(new React\Stream\WritableResourceStream(STDOUT, $loop));
    $conn->on('data', function ($data) use ($conn) {
        echo $data;
    });    
});

$loop->run();

运行php file.php立即终止,没有任何输出。我想让它保持运行并打印它收到的内容,就像使用nc一样。我能告诉我正确的方向吗?感谢

0 个答案:

没有答案