]> git.mxchange.org Git - hub.git/commitdiff
Example UDP server/client scripts added (license: see comments on online-documentatio...
authorRoland Häder <roland@mxchange.org>
Sat, 14 Feb 2009 23:47:57 +0000 (23:47 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 14 Feb 2009 23:47:57 +0000 (23:47 +0000)
.gitattributes
chat-server.php
udp-client.php [new file with mode: 0644]
udp-inc.php [new file with mode: 0644]
udp-server.php [new file with mode: 0644]

index 71e36d256dd209284a107567a042442d65d2eb4e..9d4d1ecd8a094bd63c64541d505ca06565174aea 100644 (file)
@@ -27,3 +27,6 @@ docs/THANKS -text
 /package.sh -text
 /pdepend.sh -text
 /rebuild_doc.sh -text
+/udp-client.php -text
+/udp-inc.php -text
+/udp-server.php -text
index 8c0afd8f19860a3c864f0a4abc1109901630ccea..f228f9a0e6ad574926961b70b07b695509317b99 100644 (file)
@@ -1,6 +1,10 @@
 <?php
+
+error_reporting(E_ALL | E_STRICT);
 set_time_limit(0);
 
+require("udp-inc.php");
+
 $port = 60825;
 $host = "0.0.0.0";
 
@@ -25,7 +29,7 @@ socket_listen($main_sock);
 // add the listening socket to this list
 $clients = array($main_sock);
 
-print "[".date("m/d/Y:H:i:s", time())."]:Server listens on {$host}:{$port}\n\n";
+out(__FILE__, __LINE__, "[".date("m/d/Y:H:i:s", time())."]:Server listens on {$host}:{$port}");
 
 while (count($clients) > 0) {
        // create a copy, so $clients doesn't get modified by socket_select()
@@ -49,7 +53,7 @@ while (count($clients) > 0) {
                "There are ".(count($clients) - 1)." client(s) connected to the server\n");
 
                socket_getpeername($new_sock, $ip);
-               print "[".date("m/d/Y:H:i:s", time())."]:New client connected: {$ip}\n";
+               out(__FILE__, __LINE__, "[".date("m/d/Y:H:i:s", time())."]:New client connected: {$ip}");
 
                // Notify all chatter
                if (count($clients) > 2) {
@@ -80,7 +84,7 @@ while (count($clients) > 0) {
                        // remove client for $clients array
                        $key = array_search($read_sock, $clients);
                        unset($clients[$key]);
-                       print "[".date("m/d/Y:H:i:s", time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1)."\n";
+                       out(__FILE__, __LINE__, "[".date("m/d/Y:H:i:s", time())."]:Client from {$ip} disconnected. Left: ".(count($clients) - 1)."");
 
                        // Notify all chatter
                        if (count($clients) > 1) {
@@ -99,7 +103,7 @@ while (count($clients) > 0) {
                } elseif (in_array(trim($data), $shutdown)) {
                        // Is he allowed to shutdown?
                        if (!in_array($ip, $masters)) {
-                               print "[".date("m/d/Y:H:i:s", time())."]:Client $ip has tried to shutdown the server!\n";
+                               out(__FILE__, __LINE__, "[".date("m/d/Y:H:i:s", time())."]:Client $ip has tried to shutdown the server!");
                                socket_write($read_sock, "Server: You are not allowed to shutdown the server!\n");
                                $data = "";
                                continue;
@@ -147,11 +151,11 @@ while (count($clients) > 0) {
                        socket_write($read_sock, "\nServer: Message accepted.\n");
                } elseif ($tags != $data) {
                        // HTML codes are not allowed
-                       print "[".date("m/d/Y:H:i:s", time())."]:Client $ip has entered HTML code!\n";
+                       out(__FILE__, __LINE__, "[".date("m/d/Y:H:i:s", time())."]:Client $ip has entered HTML code!");
                        socket_write($read_sock, "Server: HTML is forbidden!\n");
                } elseif ((count($clients) == 2) && ($read_sock != $main_sock)) {
                        // No one else will hear the "chatter"
-                       print "[".date("m/d/Y:H:i:s", time())."]:Client $ip speaks with himself.\n";
+                       out(__FILE__, __LINE__, "[".date("m/d/Y:H:i:s", time())."]:Client $ip speaks with himself.");
                        socket_write($read_sock, "Server: No one will hear you!\n");
                }
        } // end of reading foreach
diff --git a/udp-client.php b/udp-client.php
new file mode 100644 (file)
index 0000000..a8c21b9
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+error_reporting(E_ALL | E_STRICT);
+
+define('ROUNDS', 3);
+define('MAX_FAILURES', 3);
+
+require("udp-inc.php");
+
+global $hasher;
+
+function validate ($data) {
+       global $hasher;
+       switch ($hasher) {
+               case "md5":
+                       return md5($data);
+                       break;
+
+               case "sha1":
+                       return sha1($data);
+                       break;
+
+               case "crc32":
+                       return crc32($data);
+                       break;
+
+               default:
+                       print("Unknown hasher: ${hasher}\n");
+                       break;
+       }
+}
+
+$data = array();
+$cnt = 0;
+$invalid = 0;
+$failed = 0;
+
+$client = stream_socket_client("udp://192.168.1.1:1113", $errno, $errstr);
+
+if ((!is_resource($client)) || ($errno > 0)) {
+       echo "ERROR: $errno - $errstr\n";
+       exit;
+}
+
+if (!stream_set_blocking($client, 0)) {
+       echo "ERROR: Cannot set non-blocking mode!\n";
+       exit;
+}
+
+out(__FILE__, __LINE__, "Starting test...");
+while (!feof($client)) {
+       //out(__FILE__, __LINE__, "Sending ping...");
+       fwrite($client, "PING");
+
+       //out(__FILE__, __LINE__, "Reading reply...");
+       $read = trim(fread($client, 50));
+       if (empty($read)) {
+               $failed++;
+               out(__FILE__, __LINE__, "Empty line received. Is the server there?");
+       } elseif ($read == "INVALID") {
+               $failed++;
+               out(__FILE__, __LINE__, "Server has not accepted our message.");
+       } else {
+               $failed = 0;
+               //out(__FILE__, __LINE__, "Response ${read} received.");
+       }
+
+       if ($failed == constant('MAX_FAILURES')) {
+               out(__FILE__, __LINE__, "Too many failures! (failed=${failed})");
+               break;
+       } elseif ($failed < constant('MAX_FAILURES')) {
+               continue;
+       }
+
+       $rec  = explode(":", $read);
+
+       $time   = $rec[0];
+       $right  = explode("=", $rec[1]);
+       $hash   = $right[1];
+       $hasher = $right[0];
+
+       if (validate($time) != $hash) {
+               out(__FILE__, __LINE__, "Invalid: ${read}/{$hash}");
+               $invalid++;
+               continue;
+       }
+
+       if (!isset($data[$rec[0]])) {
+               if (count($data) > 0) {
+                       echo $data[$rec[0]-1]."\n";
+                       $cnt++;
+                       if ($cnt > constant('ROUNDS')) break;
+               }
+               $data[$rec[0]] = 0;
+       }
+       $data[$rec[0]]++;
+}
+
+array_shift($data);
+
+stream_socket_shutdown($client, STREAM_SHUT_RDWR);
+
+$avg = 0;
+$min = 0;
+$max = 0;
+
+foreach ($data as $cnt) {
+       if ($cnt > $max) {
+               $max = $cnt;
+       }
+       if (($cnt < $min) || ($min == 0)) {
+               $min = $cnt;
+       }
+       $avg += $cnt;
+}
+
+if (count($data) > 0) {
+       $avg = round($avg / count($data));
+}
+
+out(__FILE__, __LINE__, "MIN/AVG/MAX=${min}/${avg}/${max}");
+out(__FILE__, __LINE__, "INVALID=${invalid}");
+
+?>
diff --git a/udp-inc.php b/udp-inc.php
new file mode 100644 (file)
index 0000000..b92a35c
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+global $last_msg;
+$last_msg = "";
+
+function out ($file, $line, $message, $displayDouble = false) {
+       global $last_msg;
+       if (($last_msg != $message) || ($displayDouble)) {
+               print(basename($file)."[${line}]: ${message}\n");
+               $last_msg = $message;
+       }
+}
+
+?>
diff --git a/udp-server.php b/udp-server.php
new file mode 100644 (file)
index 0000000..b526092
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+require("udp-inc.php");
+
+error_reporting(E_ALL | E_STRICT);
+
+out(__FILE__, __LINE__, "Opening server port...");
+$socket = stream_socket_server("udp://192.168.1.1:1113", $errno, $errstr, STREAM_SERVER_BIND);
+if ((!is_resource($socket)) || ($errno > 0)) {
+       die("$errstr ($errno)\n");
+}
+
+$pkt = "";
+
+out(__FILE__, __LINE__, "Waiting for clients...");
+
+do {
+       $pkt = stream_socket_recvfrom($socket, 50, 0, $peer);
+       //out(__FILE__, __LINE__, "Received packet ${pkt} from peer ${peer}.");
+
+       if (trim($pkt) == "PING") {
+               //out(__FILE__, __LINE__, "Sending data to peer ${peer}.");
+               stream_socket_sendto($socket, (time().":md5=".md5(time())), 0, $peer);
+       } else {
+               out(__FILE__, __LINE__, "Invalid packet ${pkt} from peer ${peer}.");
+               stream_socket_sendto($socket, "INVALID", 0, $peer);
+       }
+} while ($pkt !== false);
+
+?>