<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '127.0.0.1', 100);
socket_listen($socket);
$client = socket_accept($socket);
socket_write($client, 'output');
sleep(10);
socket_close($client);
socket_close($socket);
?>
When I go to the terminal and type nc localhost 100
I get output immediately, as I want.
But when I type "localhost:100
" in the address bar in the browser, the word output is showed up after 10 seconds.
But!
If I change the code to:
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '127.0.0.1', 100);
socket_listen($socket);
$client = socket_accept($socket);
$str = '';
for ($index = 0; $index < 4096; $index++) {
$str .= ' ';
}
socket_write($client, $str);
socket_write($client, 'output');
sleep(10);
socket_close($client);
socket_close($socket);
?>
I get output (and all the spaces before it) on the browser immediately after sending the request.
How can I get the output immediately in the browser without sending a lot of data?