I am trying to send a tcp message in a script with reading from stdin in a loop. The problem is that the server only receives the connection, not the message. Everything works if I remove the loop.
#!/bin/php
<?php
use React\Socket\ConnectionInterface;
use React\Socket\TcpConnector;
require dirname(__DIR__) . '/vendor/autoload.php';
while (true) {
$line = trim(fgets(STDIN));
echo "try to send $line\n";
try {
$client = new TcpConnector();
$client
->connect('tcp://127.0.0.1:8080')
->then(
function (ConnectionInterface $connection) use ($line) {
echo "fulfilled\n";
$connection->write($line);
$connection->end();
},
function () {
echo "rejected\n";
},
function () {
echo "onProgress\n";
}
);
} catch (Throwable $e) {
echo $e->getMessage();
}
// break; // work with this break
}
with this code, when I send
./console.php
foo
try to send foo
bar
try to send bar
On the server side :
new connection: tcp://127.0.0.1:40936
new connection: tcp://127.0.0.1:40944
If I remove the loop :
./console.php
foo
try to send foo
fulfilled
Note: work great with telnet