Skip to content

Instantly share code, notes, and snippets.

@Rotron
Last active June 29, 2025 13:10
Show Gist options
  • Save Rotron/f2428c34418e0bfd7e53618a13d77d78 to your computer and use it in GitHub Desktop.
Save Rotron/f2428c34418e0bfd7e53618a13d77d78 to your computer and use it in GitHub Desktop.

Revisions

  1. Rotron revised this gist Jan 9, 2020. 1 changed file with 35 additions and 34 deletions.
    69 changes: 35 additions & 34 deletions server.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/local/bin/php -q
    <?php
    //a little bit of fun with a socket server
    error_reporting(E_ALL);
    /* Allow the script to hang around waiting for connections. */
    @@ -10,6 +10,11 @@
    ob_implicit_flush();
    $address = '0.0.0.0';
    $port = 10000;

    $bash = "Rotron.net > ";



    if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    die();
    @@ -44,22 +49,22 @@
    // Handle new Connections
    if (in_array($sock, $read)) {

    if (($msgsock = socket_accept($sock)) === false) {
    echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    break;
    }
    $clients[] = $msgsock;
    if (($msgsock = socket_accept($sock)) === false) {
    echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    break;
    }
    $clients[] = $msgsock;
    $key = array_keys($clients, $msgsock);

    $msg="Connected\n";
    $msg="Connected\n";

    socket_write($msgsock, $msg, strlen($msg));
    socket_write($msgsock, $msg, strlen($msg));



    /* Send instructions. */
    $msg = "\n
    /* Send instructions. */
    $msg = "\n
    ██▀███ ▒█████ ▄▄▄█████▓ ██▀███ ▒█████ ███▄ █ ███▄ █ ▓█████▄▄▄█████▓
    ▓██ ▒ ██▒▒██▒ ██▒▓ ██▒ ▓▒▓██ ▒ ██▒▒██▒ ██▒ ██ ▀█ █ ██ ▀█ █ ▓█ ▀▓ ██▒ ▓▒
    @@ -72,12 +77,12 @@
    ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
    ".
    "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));
    }
    foreach ($clients as $key => $client) { // for each client
    "To quit, type 'quit'. To shut down the server type 'shutdown'.\n" . $bash;
    socket_write($msgsock, $msg, strlen($msg));
    }
    foreach ($clients as $key => $client) { // for each client
    if (in_array($client, $read)) {
    if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
    echo "socket_read() Error: " . socket_strerror(socket_last_error($client)) . "\n";
    @@ -93,6 +98,9 @@
    continue;
    }
    if ($buf == 'quit') {

    $bye="\n Bye Bye! \n";
    socket_write($msgsock, $bye, strlen($bye));
    unset($clients[$key]);
    socket_close($client);
    break;
    @@ -103,18 +111,14 @@
    break 2;
    }
    if ($buf == 'date') {
    $data= date("Y-m-d"). "\n";
    socket_write($msgsock, $data, strlen($data));

    }
    if ($buf == '^[[A') {
    $data= "Left";
    $data= date("Y-m-d"). "\n" . $bash;
    socket_write($msgsock, $data, strlen($data));

    }


    if ($buf == 'clear') {
    $data= chr(27).chr(91).'H'.chr(27).chr(91).'J';
    $data= chr(27).chr(91).'H'.chr(27).chr(91).'J' . $bash;
    socket_write($msgsock, $data, strlen($data));

    }
    @@ -141,30 +145,27 @@
    └─────────────┴──────────────────────────────────────────────────────────┘
    ";




    socket_write($msgsock, $data, strlen($data));
    "
    . $bash;
    socket_write($msgsock, $data, strlen($data));

    }
    /*
    $talkback = "Client {$key}: said '$buf'.\n";
    foreach ($clients as $sub_key => $sub_client)
    {
    socket_write($sub_client, $talkback, strlen($talkback));
    }
    socket_write($sub_client, $talkback, strlen($talkback));
    }
    echo "$buf\n";
    //exec('say "'. $buf . '"');
    */
    }

    }
    } while (true);
    socket_close($sock);
    ?>
  2. Rotron revised this gist Jan 9, 2020. 1 changed file with 1 addition and 43 deletions.
    44 changes: 1 addition & 43 deletions server.php
    Original file line number Diff line number Diff line change
    @@ -71,48 +71,6 @@
    ░░ ░ ░ ░ ░ ▒ ░ ░░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░
    ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
    ".
    "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));
    @@ -183,7 +141,7 @@
    └─────────────┴──────────────────────────────────────────────────────────┘
    ";
    ";



  3. Rotron created this gist Jan 9, 2020.
    212 changes: 212 additions & 0 deletions server.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,212 @@
    #!/usr/local/bin/php -q
    <?php

    //a little bit of fun with a socket server
    error_reporting(E_ALL);
    /* Allow the script to hang around waiting for connections. */
    set_time_limit(0);
    /* Turn on implicit output flushing so we see what we're getting
    * as it comes in. */
    ob_implicit_flush();
    $address = '0.0.0.0';
    $port = 10000;
    if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    die();
    }
    if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    die();
    }
    if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    die();
    }
    //clients array
    $clients = array();
    do {

    $read = array();
    $read[] = $sock;

    $read = array_merge($read,$clients);

    $write = NULL;
    $except = NULL;

    // Set up a blocking call to socket_select
    if(socket_select($read,$write, $except, $tv_sec = 5) < 1)
    {
    // SocketServer::debug("Problem blocking socket_select?");
    continue;
    }

    // Handle new Connections
    if (in_array($sock, $read)) {

    if (($msgsock = socket_accept($sock)) === false) {
    echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    break;
    }

    $clients[] = $msgsock;
    $key = array_keys($clients, $msgsock);

    $msg="Connected\n";

    socket_write($msgsock, $msg, strlen($msg));



    /* Send instructions. */
    $msg = "\n
    ██▀███ ▒█████ ▄▄▄█████▓ ██▀███ ▒█████ ███▄ █ ███▄ █ ▓█████▄▄▄█████▓
    ▓██ ▒ ██▒▒██▒ ██▒▓ ██▒ ▓▒▓██ ▒ ██▒▒██▒ ██▒ ██ ▀█ █ ██ ▀█ █ ▓█ ▀▓ ██▒ ▓▒
    ▓██ ░▄█ ▒▒██░ ██▒▒ ▓██░ ▒░▓██ ░▄█ ▒▒██░ ██▒▓██ ▀█ ██▒ ▓██ ▀█ ██▒▒███ ▒ ▓██░ ▒░
    ▒██▀▀█▄ ▒██ ██░░ ▓██▓ ░ ▒██▀▀█▄ ▒██ ██░▓██▒ ▐▌██▒ ▓██▒ ▐▌██▒▒▓█ ▄░ ▓██▓ ░
    ░██▓ ▒██▒░ ████▓▒░ ▒██▒ ░ ░██▓ ▒██▒░ ████▓▒░▒██░ ▓██░ ██▓ ▒██░ ▓██░░▒████▒ ▒██▒ ░
    ░ ▒▓ ░▒▓░░ ▒░▒░▒░ ▒ ░░ ░ ▒▓ ░▒▓░░ ▒░▒░▒░ ░ ▒░ ▒ ▒ ▒▓▒ ░ ▒░ ▒ ▒ ░░ ▒░ ░ ▒ ░░
    ░▒ ░ ▒░ ░ ▒ ▒░ ░ ░▒ ░ ▒░ ░ ▒ ▒░ ░ ░░ ░ ▒░ ░▒ ░ ░░ ░ ▒░ ░ ░ ░ ░
    ░░ ░ ░ ░ ░ ▒ ░ ░░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░
    ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
    ".
    "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));
    }


    foreach ($clients as $key => $client) { // for each client
    if (in_array($client, $read)) {
    if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
    echo "socket_read() Error: " . socket_strerror(socket_last_error($client)) . "\n";
    break 2;
    }
    if (!$buf = trim($buf)) {

    continue;
    }

    if ($buf == "\n") {

    continue;
    }
    if ($buf == 'quit') {
    unset($clients[$key]);
    socket_close($client);
    break;
    }
    if ($buf == 'shutdown') {
    socket_shutdown($client, 2);
    socket_close($client);
    break 2;
    }
    if ($buf == 'date') {
    $data= date("Y-m-d"). "\n";
    socket_write($msgsock, $data, strlen($data));

    }
    if ($buf == '^[[A') {
    $data= "Left";
    socket_write($msgsock, $data, strlen($data));

    }

    if ($buf == 'clear') {
    $data= chr(27).chr(91).'H'.chr(27).chr(91).'J';
    socket_write($msgsock, $data, strlen($data));

    }




    if ($buf == 'help') {
    $data= "\n
    ┌─────────────┬──────────────────────────────────────────────────────────┐
    │ Command │ Description │
    ├─────────────┼──────────────────────────────────────────────────────────┤
    │ quit │ Close the connection │
    ├─────────────┼──────────────────────────────────────────────────────────┤
    │ shutdown │ Shutdown the server │
    ├─────────────┼──────────────────────────────────────────────────────────┤
    │ date │ Date time │
    ├─────────────┼──────────────────────────────────────────────────────────┤
    │ │ │
    ├─────────────┼──────────────────────────────────────────────────────────┤
    │ │ │
    ├─────────────┼──────────────────────────────────────────────────────────┤
    │ help │ Help commands list │
    └─────────────┴──────────────────────────────────────────────────────────┘
    ";




    socket_write($msgsock, $data, strlen($data));

    }
    /*
    $talkback = "Client {$key}: said '$buf'.\n";
    foreach ($clients as $sub_key => $sub_client)
    {
    socket_write($sub_client, $talkback, strlen($talkback));
    }
    echo "$buf\n";
    //exec('say "'. $buf . '"');
    */
    }

    }

    } while (true);
    socket_close($sock);
    ?>