3 error_reporting(E_ALL | E_STRICT);
6 require('udp-inc.php');
11 $leaving = array('/leave', '/logout', '/bye', '/quit');
12 $shutdown = array('/shutdown', '/halt');
13 $masters = array('127.0.0.1');
15 // create a streaming socket, of type TCP/IP
16 $main_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
18 // set the option to reuse the port
19 socket_set_option($main_sock, SOL_SOCKET, SO_REUSEADDR, 1);
21 // "bind" the socket to the address to "localhost", on port $port
22 // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc..
23 socket_bind($main_sock, $host, $port);
25 // start listen for connections
26 socket_listen($main_sock);
28 // create a list of all the clients that will be connected to us..
29 // add the listening socket to this list
30 $clients = array($main_sock);
32 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Server listens on {$host}:{$port}");
34 while (count($clients) > 0) {
35 // create a copy, so $clients doesn't get modified by socket_select()
38 // get a list of all the clients that have data to be read from
39 // if there are no clients with data, go to next iteration
40 $left = @socket_select($read, $write = null, $except = null, 0, 150);
45 // check if there is a client trying to connect
46 if (in_array($main_sock, $read)) {
47 // accept the client, and add him to the $clients array
48 $new_sock = socket_accept($main_sock);
49 $clients[] = $new_sock;
51 // send the client a welcome message
52 socket_write($new_sock, "No noobs, but I'll make an exception. :)\n".
53 "There are ".(count($clients) - 1)." client(s) connected to the server.\n");
55 socket_getpeername($new_sock, $ip);
56 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:New client connected: {$ip}");
59 if (count($clients) > 2) {
60 foreach ($clients as $send_sock) {
61 if ($send_sock != $main_sock && $send_sock != $new_sock) {
62 socket_write($send_sock, "Server: Chatter has joined from {$ip}. There are now ".(count($clients) - 1)." clients.\n");
67 // remove the listening socket from the clients-with-data array
68 $key = array_search($main_sock, $read);
72 // loop through all the clients that have data to read from
73 foreach ($read as $read_sock) {
75 socket_getpeername($read_sock, $ip);
77 // read until newline or 1024 bytes
78 // socket_read while show errors when the client is disconnected, so silence the error messages
79 $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
81 // check if the client is disconnected
82 if (($data === FALSE) || (in_array(strtolower(trim($data)), $leaving))) {
84 // remove client for $clients array
85 $key = array_search($read_sock, $clients);
86 unset($clients[$key]);
87 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1)."");
90 if (count($clients) > 1) {
91 foreach ($clients as $send_sock) {
92 if ($send_sock != $main_sock) {
93 socket_write($send_sock, "Server: Chatter from {$ip} has logged out. ".(count($clients) - 1)." client(s) left.\n");
98 // continue to the next client to read from, if any
99 socket_write($read_sock, "Server: Good bye.\n");
100 socket_shutdown($read_sock, 2);
101 socket_close($read_sock);
103 } elseif (in_array(trim($data), $shutdown)) {
104 // Is he allowed to shutdown?
105 if (!in_array($ip, $masters)) {
106 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has tried to shutdown the server!");
107 socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n");
112 // Close all connections a leave here
113 foreach ($clients as $client) {
114 // Send message to client
115 if ($client !== $main_sock && $client != $read_sock) {
116 socket_write($client, "Server: Shutting down! Thank you for joining us.\n");
120 socket_shutdown($client, 2);
121 socket_close($client);
130 // trim off the trailing/beginning white spaces
133 // Test for HTML codes
134 $tags = strip_tags($data);
136 // check if there is any data after trimming off the spaces
137 if (!empty($data) && $tags == $data && count($clients) > 2) {
138 // Send confirmation to "chatter"
139 socket_write($read_sock, "\nServer: Message accepted.\n");
141 // send this to all the clients in the $clients array (except the first one, which is a listening socket)
142 foreach ($clients as $send_sock) {
144 // if its the listening sock or the client that we got the message from, go to the next one in the list
145 if ($send_sock == $main_sock || $send_sock == $read_sock)
148 // write the message to the client -- add a newline character to the end of the message
149 socket_write($send_sock, "{$ip}:{$data}\n");
151 } // end of broadcast foreach
152 } elseif ($tags != $data) {
153 // HTML codes are not allowed
154 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} has entered HTML code!");
155 socket_write($read_sock, "Server: HTML is forbidden!\n");
156 } elseif ((count($clients) == 2) && ($read_sock != $main_sock)) {
157 // No one else will hear the "chatter"
158 out(__FILE__, __LINE__, '['.date('m/d/Y:H:i:s', time())."]:Client {$ip} speaks with himself.");
159 socket_write($read_sock, "Server: No one will hear you!\n");
161 } // end of reading foreach
164 // close the listening socket
165 socket_close($main_sock);