rendered paste body<?php
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";
$client = array();
/* create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock, $address, $port);
socket_listen($mysock, 5);
socket_set_nonblock($mysock);
while (true) {
//usleep(1000000);
//echo time() . ' ' . $mysock . "\n";
$tmp['uid'] = rand('1000', '9999');
while (in_array($tmp['uid'], $client)) {
$tmp['uid'] = rand('1000', '9999');
}
//print_r($client);
//print_r($tmp);
if (($tmp['socket'] = @socket_accept($mysock))) {
socket_set_nonblock($tmp['socket']);
echo 'Client connected.' . "\n";
$client[$tmp['uid']]['buffer'] = $NULL;
$client[$tmp['uid']]['socket'] = &$tmp['socket'];
unset($tmp['socket']);
$client[$tmp['uid']]['ssid'] = md5(rand('100000', '999999'));
$msg = 'Welcome, your ssid is assigned ' . $client[$tmp['uid']]['ssid'] . '.' . "\n\r\n\r" . 'Please make note of this.' . "\n\r\n\r";
echo 'Message sent to ' . $client[$tmp['uid']]['ssid'] . ' saying ' . $msg;
socket_write($client[$tmp['uid']]['socket'], $msg, strlen($msg));
// echo ':irc.eLearn.net NOTICE AUTH :*** Found your hostname (cached)';
//echo ':irc.eLearn.net NOTICE Daemon :*** If you are having problems connecting due to ping timeouts, please type /quote pong D602B198 or /raw pong D602B198 now.' . "\n\r";
//echo 'Sending Request...' . "\n\r";
//$msg = 'Welcome to eLearn.' . "\n\r\n\r";
//$msg .= 'Please provide identification...' . "\n\r";
//socket_write ($tmpsock, $msg, strlen($msg));
}
foreach ($client as $key => $uid) {
//print 'Socket: ' . $client[$key]['ssid'] . "\n";
if (($tmp['buffer'] = socket_read($client[$key]['socket'], 1024))) {
//echo $client[$key]['buffer'] . "\n";
$client[$key]['buffer'] .= $tmp['buffer'];
}
if ($tmp['pos'] = strrpos($client[$key]['buffer'], "\n")) {
$client[$key]['idle'] = time();
echo $client[$key]['buffer'] . "\n";
$tmp['buffer'] = substr($client[$key]['buffer'], 0, $tmp['pos']) . "\n\r\n\r";
socket_write($client[$key]['socket'], $tmp['buffer'], strlen($tmp['buffer']));
$client[$key]['buffer'] = substr($client[$key]['buffer'], $tmp['pos'] + 1);
}
if ($tmp['pos'] = strrpos($client[$key]['buffer'], "\x08")) {
$client[$key]['buffer'] = substr($client[$key]['buffer'], 0, -2) ;
}
}
}
echo "ERROR: Server Halted";
?>