I want to show $input from the client using echo on the server side.
PHP Server:
<?php
$socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
$input = fread($conn, 1024);
echo $input;
fwrite($conn, 'Wait for a while... ' . $input);
fclose($conn);
}
fclose($socket);
}
fwrite() successfully writes $input to client but echo $input displaying nothing.
You should use flush():
<?php
$socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
$input = fread($conn, 1024);
echo $input;
ob_flush();
flush();
fwrite($conn, 'Wait for a while... ' . $input);
fclose($conn);
}
fclose($socket);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With