Ilya I
Ilya I

Reputation: 1282

Socket server displaying received data on the page

I need a script that listening for connections and outputs received data. I've tried following code:

header('Content-Type: text/plain; charset=utf-8');
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
if (!$socket) {
    echo "$errstr ($errno)\n";
} else {
    while ($conn = stream_socket_accept($socket)) {
        $data = fread($conn, 100);
        var_dump($data);
        flush();
        fclose($conn);
    }
    fclose($socket);
}

based on this example: php.net/manual/en/function.stream-socket-server.php

The problem is that nothing appears on the page when a client connects and sends some data. Using buffering prevention techniques like ob_implicit_flush(), and even setting output_buffering = Off in php.ini won't help. Might the buffering in browser leads to this and is there anything else I should try?

Upvotes: 0

Views: 196

Answers (1)

Ilya I
Ilya I

Reputation: 1282

As I thought, it happened due to Chrome browser displaying output only when connection is closed. Unfortunately there seems no easy way to change this behavior in Chrome.

Upvotes: 1

Related Questions