Skip to content

Instantly share code, notes, and snippets.

@sshilko
Created March 27, 2016 15:34
Show Gist options
  • Save sshilko/443ae40a4d115ca8325b to your computer and use it in GitHub Desktop.
Save sshilko/443ae40a4d115ca8325b to your computer and use it in GitHub Desktop.

Revisions

  1. sshilko created this gist Mar 27, 2016.
    44 changes: 44 additions & 0 deletions client.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    <?php
    error_reporting(E_ALL);
    send_message('127.0.0.1','8000', getmypid() . ' Message to send...');

    function send_message($ipServer,$portServer,$message)
    {
    $fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);
    stream_set_blocking($fp, false);
    if (1) {
    while(1) {
    $wr = array($fp);
    $rr = array($fp);
    $nl = null;

    if ($ss = stream_select($nl, $wr, $nl, 1, 0) > 0) {
    echo 'SS result=';
    var_dump($ss);
    $written = fwrite($fp, "$message" . time() . "\n");
    echo 'Written result:';
    var_dump($written);
    if ($written === false) {
    echo "FAILED TO WRITE";
    sleep(1);
    }
    if ($written === 0) {
    $info = stream_get_meta_data($fp);
    print_r($info);
    if (feof($fp)) {

    echo 'FEOF detected';
    }

    sleep(1);

    }
    echo 'Written=' . $written . "\n";
    sleep(1);
    } else {
    echo 'cant select to write';
    }
    }

    }
    }
    29 changes: 29 additions & 0 deletions server.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?php

    $socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr);

    echo "Dispatching...\n";
    pcntl_signal_dispatch();

    register_shutdown_function(function($s) {
    echo 'Doing shutdown';
    @fclose($s);
    }, $socket);

    while(1) {
    $conn = @stream_socket_accept($socket, 10);
    if ($conn) {
    while($data = fread($conn, 1024)) {
    if ($data) {
    echo "RCV: " . $data;
    }
    }
    @fclose($conn);
    } else {
    break;

    }
    }


    @fclose($socket);