All pastes #639497 Raw Edit

Anonymous

public text v1 · immutable
#639497 ·published 2007-07-30 16:50 UTC
rendered paste body
SERVER

#!/usr/bin/php-gtk
<?php

define('EOL',"\r\n");

$server = stream_socket_server("tcp://localhost:1212", $errno, $errstr);

if (!$server) {
  echo "$errstr ($errno)\n";
  exit(0);
}

Gtk::io_add_watch($server, Gtk::IO_IN, 'accept_client');

function accept_client($server, $type){
  $client = stream_socket_accept($server);
  fwrite($client, 'DocSynch'.chr(0).'READY'.EOL);
  echo "DocSynch READY\n";
  Gtk::io_add_watch($client, Gtk::IO_IN, 'await_auth');
  return true;
}

function await_auth($client, $type){
  $cmd = explode(chr(0),fgets($client));
  print_r($cmd);
  if($cmd[0] === 'AUTH'){
    fwrite($client, 'ERR'.chr(0).'AUTH'.EOL);
    return true;
  }else{
    fclose($client);
    return false;
  }
}

?>

CLIENT

#!/usr/bin/php-gtk
<?php

define('EOL',"\r\n");

$fp = stream_socket_client("tcp://localhost:1212", $errno, $errstr);
if (!$fp) {
    echo "$errstr ($errno)\n";
} else {
    while (!feof($fp)) {
      $cmd = explode(chr(0),fgets($fp));
      print_r($cmd);
      if($cmd[0] === 'DocSynch' && $cmd[1] == 'READY'.EOL){
        fwrite($fp, 'AUTH'.chr(0).'leon_pegg'.chr(0).md5('andritz').EOL);
      }
    }
    fclose($fp);
}
?>