ok well thats everybodies problem ... thats why you want this code in the first place
what is the first part of that code that does not work.. what error is thrown...
here is a rewrite of the dave.dapond.com code.. this works on my windows machine... its only the server.. you have to telnet into that port and type.. and when you are done you need to either exit with ctrl-c from the commandline where you starte the server.. or just exit from the client...
this code i know works with 4.2.3 on win XP
#!c:\php4\php.exe -q
<?php
/* a simple 'partyline' example of how to use socket_select() and co. to create
* a multi-socket php listening daemon with multi-client support.
*
* modify at will. (by dave on irc.dal.net)
*/
/* this is to be a daemon, don't want to auto-timeout and no error messages! */
set_time_limit(0);
//error_reporting(0);
echo "[server start] program started\n";flush();
$listening_address = '127.0.0.1';
$listening_port = 10000;
define('STATE_NICKNAME', 1);
define('STATE_CHATTING', 2);
echo "[server start] configuration set\n";flush();
/* display error message */
function throwError() {
echo '[error]: ' . socket_strerror(socket_last_error()) . "\n";flush();
}
/* display error message and die */
function throwFatal() {
throwError();
die();
}
/* send a message to a socket */
function writeSocket ( $socket, $message )
{
socket_write($socket, "$message\n");
}
/* send a message to all client sockets */
function writeAll ( $message, $state = STATE_CHATTING )
{
global $clients;
global $data;
foreach ( $clients as $client ) {
if ( $data[$client]['state'] == $state ||
$state == 0
)
{
writeSocket( $client, $message );
}
}
}
/* send a message to all client sockets except restricted ones */
function writeRestricted( $restricted_sockets, $message, $state = STATE_CHATTING )
{
global $clients;
global $data;
if ( !is_array($restricted_sockets) ) {
$restricted_sockets = array( $restricted_sockets );
}
foreach ( $clients as $client ) {
if ( !in_array($client,$restricted_sockets) &&
( $data[$client]['state'] == $state ||
$state == 0
) )
{
writeSocket($client, $message);
}
}
}
/* handle incoming data */
function handleIncomingData( $socket, $incoming_data )
{
global $data;
$data[$socket]['buffer'] = $incoming_data . $data[$socket]['buffer'];
/*
if ( $data[$socket]['buffer'] != '' ) {
$incoming_data = $data[$socket]['buffer'] . $incoming_data;
}
$incoming_data = str_replace("\r", '', $incoming_data);
$data[$socket]['buffer'] = substr(strrchr($incoming_data, "\n"), 1);
$incoming_data = substr($incoming_data, 0, strpos($incoming_data, "\n"));
*/
if ( $data[$socket]['state'] == STATE_NICKNAME ) {
actionNickname($socket, $incoming_data);
} else {
actionChat($socket, $incoming_data);
}
}
/* handle nickname entry */
function actionNickname($socket, $nickname)
{
global $clients;
global $data;
$nickname = trim($nickname);
/*
if ( !preg_match('/^\w{1,10}$/', $nickname)) {
writeSocket($socket, "That nick is invalid [{$nickname}]. Please choose another:");
return;
}
*/
foreach ( $clients as $client ) {
if ( $data[$client]['nickname']==$nickname ) {
writeSocket($socket, "That nick is already in use. Please choose another:");
return;
}
}
$data[$socket]['nickname'] = $nickname;
$data[$socket]['state'] = STATE_CHATTING;
actionJoin($socket);
}
/* handle chat text */
function actionChat ( $socket, $incoming_data )
{
global $data;
/* do who command */
if ( $incoming_data == '.who') {
actionWho($socket);
return;
/* check for error */
} else if ($incoming_data{0} == '.') {
sendSocket($socket, "Invalid command.");
return;
}
$nick = $data[$socket]['nickname'];
// writeRestricted( array($socket), "<{$data[$socket]['nickname']}> $data");
writeAll( "<{$data[$socket]['nickname']}> $incoming_data");
}
/* client has joined */
function actionJoin( $socket )
{
global $data;
writeAll("*** :) {$data[$socket]['nickname']} has joined the chat ({$data[$socket]['ip_address']})");
}
/* client has quit */
function actionQuit ( $socket )
{
global $data;
writeAll("*** :( {$data[$socket]['nickname']} has left the chat ({$data[$socket]['ip_address']})");
}
/* who command */
function actionWho ( $socket )
{
global $clients;
global $data;
foreach ( $clients as $client ) {
if ( $data[$client]['state'] == STATE_NICKNAME )
writeSocket( $socket, "[nickname] ({$data[$client]['ip_address']})" );
else if ( $data[$client]['state'] == STATE_CHATTING )
writeSocket( $socket, "[chatting] {$data[$client]['nickname']} ({$data[$client]['ip_address']})" );
else
writeSocket( $socket, "[ghost] {$data[$client]['nickname']} ({$data[$client]['ip_address']})" );
}
}
echo "[server start] functions defined\n";flush();
/* assign listening socket */
$listening_socket = socket_create(AF_INET, SOCK_STREAM, 0)
or throwFatal();
echo "[server start] listening socket initialized\n";flush();
/* reuse listening socket address */
socket_setopt($listening_socket, SOL_SOCKET, SO_REUSEADDR, 1)
or throwFatal();
echo "[server start] listening socket set to resuse address\n";flush();
/* set socket to non-blocking */
socket_set_nonblock($listening_socket)
or throwFatal();
echo "[server start] listening socket set to non-blocking mode\n";flush();
/* bind listening socket to specific address/port */
socket_bind($listening_socket, $listening_address, $listening_port)
or throwFatal();
echo "[server start] listening socket bound to {$listening_address}:{$listening_port} \n";flush();
/* listen on listening socket */
socket_listen($listening_socket)
or throwFatal();
echo "[server start] listening socket told to listen \n";flush();
/* set initial vars and loop until $abort is set to true */
$clients = array();
$data = array();
$abort = FALSE;
echo "[server start] starting loop of death \n";flush();
while ( !$abort ) {
/* sockets we want to pay attention to */
$set = array_merge($listening_socket, $clients );
$empty_write = array();
$empty_read = array();
if (socket_select($set, $empty_write, $empty_read, 1, 0) > 0) {
echo "[socket noise]\n";flush();
/* loop through sockets */
foreach ( $set as $socket ) {
/* listening socket has a connection, deal with it */
if ( $socket == $listening_socket ) {
echo " [new client] noise on $listening_socket \n";flush();
/* get the connection to the new client */
$new_client_socket = socket_accept($listening_socket)
or fatalError();
echo " [new client] accepted new client socket ($new_client_socket)\n";flush();
/* add socket to client list and announce connection */
socket_getpeername($new_client_socket, $ip_address);
$clients[$new_client_socket] = $new_client_socket;
$data[$new_client_socket] = array(
'ip_address' => $ip_address,
'buffer' => '',
'state' => STATE_NICKNAME
);
echo " [new client] connected from $ip_address\n";flush();
writeSocket($new_client_socket, 'Enter a nickname:');
/* a client socket has incoming data */
} else {
echo " [client] noise on $socket \n";flush();
/* no error, but connection was closed, so tell everyone */
if ( ($incoming_data = socket_read($socket, 1024) ) === FALSE || $incoming_data == '' ) {
actionQuit($socket);
// remove client from arrays
unset($clients[$socket]);
unset($data[$socket]);
echo "[client data] closing socket $incoming_data \n";exit;
/* must be some good data from client */
} else {
echo "[client data] $incoming_data \n";
/* only want data with a newline */
if (strchr($incoming_data, "\n") === false) {
$data[$socket]['buffer'] .= $incoming_data;
} else {
handleIncomingData($socket, $incoming_data);
}
} /// handle data
} /// handle listening:client socket
} // foreach client
} // if noise on sockets
} // while not aborting
echo "[end]\n";flush();
exit;
?>