#!/usr/local/bin/php -q You have disconnected." . chr (0)); else if ($client [$broadcast_count] != null) socket_write ($client [$broadcast_count], " " . $usernames [$count] . " has disconnected." . chr (0)); } socket_close ($client [$count]); $client [$count] = null; unset ($remote_host [$count]); $client_still_connected = false; for ($count = 0; $count <= MAX_CLIENTS; $count++) { if ($client [$count] != null) { $client_still_connected = true; break; } } if ($client_still_connected == false) kill_server (); } //--------------------------------------------- $listenfd = socket_create (AF_INET, SOCK_STREAM, 0); if ($listenfd) print "Listening on port " . PORT . "\n"; else { socket_write ($client [$count], "Socket died!" . chr (0)); die ("Socket died!\n"); } socket_setopt ($listenfd, SOL_SOCKET, SO_REUSEADDR, 0); if (!socket_bind ($listenfd, "127.0.0.1", PORT)) { socket_close ($listenfd); die ("Could not bind!\n"); } socket_listen ($listenfd, LISTEN_QUEUE); for ($count = 0; $count < MAX_CLIENTS; $count++) $client [$count] = null; while (1) { $rfds [0] = $listenfd; { for ($count = 0; $count <= MAX_CLIENTS; $count++) if ($client [$count] != null) $rfds [$count + 1] = $client [$count]; } $nready = socket_select ($rfds, $null, $null, null); if (in_array ($listenfd, $rfds)) { for ($count = 0; $count <= MAX_CLIENTS; $count++) { if ($client [$count] == null) { $client [$count] = socket_accept ($listenfd); socket_setopt ($client [$count], SOL_SOCKET, SO_REUSEADDR, 0); socket_getpeername ($client [$count], $remote_host [$count], $remote_port [$count]); if ($count < MAX_CLIENTS) { break; } } if ($count >= MAX_CLIENTS) { close_client ($count); break; } } if (--$nready <= 0) continue; } for ($count = 0; $count <= MAX_CLIENTS; $count++) { if ($client [$count] == null) continue; if (in_array ($client [$count], $rfds)) { $incoming_data = trim (socket_read ($client [$count], SOCKET_READ)); if (!$incoming_data) close_client ($count); else { if (substr ($incoming_data, 0, 1) == "#") { $call_function = explode ("#", $incoming_data); if ($call_function [1] == "client_number") socket_write ($client [$count], $count + 1 . chr (0)); else if ($call_function [1] == "username") { $usernames [$count] = $call_function [2]; for ($broadcast_count = 0; $broadcast_count <= MAX_CLIENTS; $broadcast_count++) { if (($client [$broadcast_count] != null) && ($broadcast_count != $count)) socket_write ($client [$broadcast_count], " " . $usernames [$count] . " has connected." . chr (0)); } } else if ($call_function [1] == "room_join") { $room = $call_function [2]; room_join ($room); } else if ($call_function [1] == "room_exit") room_exit (); else if ($call_function [1] == "close_client") close_client ($count); else if ($call_function [1] == "kill_server") kill_server (); } else { for ($broadcast_count = 0; $broadcast_count <= MAX_CLIENTS; $broadcast_count++) { if ($broadcast_count == $count) socket_write ($client [$count], "You said: " . $incoming_data . chr (0)); else if ($client [$broadcast_count] != null) socket_write ($client [$broadcast_count], $usernames [$count] . ": " . $incoming_data . chr (0)); } } } if (--$nready <= 0) break; } } } ?>
Site hosted by Angelfire.com: Build your free website today!