A socket in PHP. Simple example

Hello everyone. You can show a simple robot socket on the PCP. A simple socket that outputs a static string without receiving data. I think it won't be difficult. And one more question. The socket works if you connect only on the desired port (for example, example. com:8000) or with any connection. This is all new to me.

Author: Fariz Mamedow, 2018-01-08

1 answers

A simple example that just listens and responds with Hi! its work! when accessed

    <?php

    $socket = stream_socket_server("tcp://0.0.0.0:8000", $errorNumber, $errorDescription);

    if (!$socket) {
        die("$errorDescription ($errorNumber)\n");
    }

    while ($connect = stream_socket_accept($socket, -1)) {
        fwrite($connect, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\nHi! its work!\n\n");
        fclose($connect);
    }

    fclose($socket);
 1
Author: StereoFlo, 2018-01-09 07:54:10