Saturday, 6 January 2007

LiveSelfChat(wow...)

Hey guys,heres something really cool.
its stolen from php.net ^^
but if im true...i was a bit too lazy to write almost the same shit again by myself =\
Whatever...this is a really nice application which will allow an user to write a message though telnet and it will be printed out on the scripts page in the same time ;)
error_reporting (E_ALL);
set_time_limit (0);
ob_implicit_flush ();
$address = '192.168.2.100';
$port = 8888;
if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() errord: Reason: " . socket_strerror ($sock) . "\n";
}

if (($ret = socket_bind ($sock, $address, $port)) < 0) {
echo "socket_bind() errord: Reason: " . socket_strerror ($ret) . "\n";
}

if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() errord: Reason: " . socket_strerror ($ret) . "\n";
}

do {
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() errord: Reason: " . socket_strerror ($msgsock) . "\n";
break;
}
/* Anweisungen senden. */
$msg = "\nWelcome. \n\r" ;
socket_write($msgsock, $msg, strlen($msg));

do {
if (FALSE === ($buf = socket_read ($msgsock, 1024))) {
echo "socket_read() fehlgeschlagen: Grund: " . socket_strerror ($ret) . "\n";
break 2;
}
if (!$buf = trim ($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close ($msgsock);
break 2;
}
$talkback = "";
socket_write ($msgsock, $talkback, strlen ($talkback));
echo "$buf";
} while (true);
socket_close ($msgsock);
} while (true);

socket_close ($sock);
?>

No comments: