#!/usr/local/bin/php -q <?php set_time_limit (0); ob_implicit_flush (); define ('SOCKET_READ', 1024); define ('LISTEN_QUEUE', 10); define ('PORT', 9999); define ('MAX_CLIENTS', 30); define ('MAX_ROOMS', 4); //--------------------------------------------- function room_join ($room) { global $client, $count, $room, $rooms, $room_position; if (!isset ($rooms)) { for ($room_build_count = 0; $room_build_count < MAX_ROOMS; $room_build_count++) $rooms [$room_build_count] = array (); } $room_size = count ($rooms [$room]); if ($room_size < 4) { array_push ($rooms [$room], $count); $room_position [$count] = array ($room, $room_size); socket_write ($client [$count], "You have joined room " . $room . "." . chr (0)); socket_write ($client [$count], $room . "#" . ($room_size + 1) . chr (0)); } else socket_write ($client [$count], "Room " . $room . " is currently full." . chr (0)); } //--------------------------------------------- function room_exit () { global $client, $count, $rooms, $room_position; array_splice ($rooms [$room_position [$count] [0]], $room_position [$count] [1], 1); socket_write ($client [$count], "You have left room " . $room_position [$count] [0] . "." . chr (0)); $room_position [$count] = null; } //--------------------------------------------- function kill_server () { global $listenfd, $client; for ($count = 0; $count < MAX_CLIENTS; $count++) { if ($client [$count] != null) { socket_write ($client [$count], "Server disconnected!" . chr (0)); socket_close ($client [$count]); } } socket_close ($listenfd); exit; } //--------------------------------------------- function close_client ($count) { global $client, $usernames, $remote_host, $remote_port; if (isset ($rooms)) room_exit (); for ($broadcast_count = 0; $broadcast_count <= MAX_CLIENTS; $broadcast_count++) { if ($broadcast_count == $count) socket_write ($client [$count], " <font color="#CC0000">You have disconnected.</font>" . chr (0)); else if ($client [$broadcast_count] != null) socket_write ($client [$broadcast_count], " <font color="#CC0000">" . $usernames [$count] . " has disconnected.</font>" . 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 . ""; else { socket_write ($client [$count], "Socket died!" . chr (0)); die ("Socket died!"); } socket_setopt ($listenfd, SOL_SOCKET, SO_REUSEADDR, 0); if (!socket_bind ($listenfd, "127.0.0.1", PORT)) { socket_close ($listenfd); die ("Could not bind!"); } 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], " <font color="#CC0000">" . $usernames [$count] . " has connected.</font>" . 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], "<font color="#999999">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; } } } ?>