]> git.mxchange.org Git - hub.git/blob - udp-client.php
Example UDP server/client scripts added (license: see comments on online-documentatio...
[hub.git] / udp-client.php
1 <?php
2 error_reporting(E_ALL | E_STRICT);
3
4 define('ROUNDS', 3);
5 define('MAX_FAILURES', 3);
6
7 require("udp-inc.php");
8
9 global $hasher;
10
11 function validate ($data) {
12         global $hasher;
13         switch ($hasher) {
14                 case "md5":
15                         return md5($data);
16                         break;
17
18                 case "sha1":
19                         return sha1($data);
20                         break;
21
22                 case "crc32":
23                         return crc32($data);
24                         break;
25
26                 default:
27                         print("Unknown hasher: ${hasher}\n");
28                         break;
29         }
30 }
31
32 $data = array();
33 $cnt = 0;
34 $invalid = 0;
35 $failed = 0;
36
37 $client = stream_socket_client("udp://192.168.1.1:1113", $errno, $errstr);
38
39 if ((!is_resource($client)) || ($errno > 0)) {
40         echo "ERROR: $errno - $errstr\n";
41         exit;
42 }
43
44 if (!stream_set_blocking($client, 0)) {
45         echo "ERROR: Cannot set non-blocking mode!\n";
46         exit;
47 }
48
49 out(__FILE__, __LINE__, "Starting test...");
50 while (!feof($client)) {
51         //out(__FILE__, __LINE__, "Sending ping...");
52         fwrite($client, "PING");
53
54         //out(__FILE__, __LINE__, "Reading reply...");
55         $read = trim(fread($client, 50));
56         if (empty($read)) {
57                 $failed++;
58                 out(__FILE__, __LINE__, "Empty line received. Is the server there?");
59         } elseif ($read == "INVALID") {
60                 $failed++;
61                 out(__FILE__, __LINE__, "Server has not accepted our message.");
62         } else {
63                 $failed = 0;
64                 //out(__FILE__, __LINE__, "Response ${read} received.");
65         }
66
67         if ($failed == constant('MAX_FAILURES')) {
68                 out(__FILE__, __LINE__, "Too many failures! (failed=${failed})");
69                 break;
70         } elseif ($failed < constant('MAX_FAILURES')) {
71                 continue;
72         }
73
74         $rec  = explode(":", $read);
75
76         $time   = $rec[0];
77         $right  = explode("=", $rec[1]);
78         $hash   = $right[1];
79         $hasher = $right[0];
80
81         if (validate($time) != $hash) {
82                 out(__FILE__, __LINE__, "Invalid: ${read}/{$hash}");
83                 $invalid++;
84                 continue;
85         }
86
87         if (!isset($data[$rec[0]])) {
88                 if (count($data) > 0) {
89                         echo $data[$rec[0]-1]."\n";
90                         $cnt++;
91                         if ($cnt > constant('ROUNDS')) break;
92                 }
93                 $data[$rec[0]] = 0;
94         }
95         $data[$rec[0]]++;
96 }
97
98 array_shift($data);
99
100 stream_socket_shutdown($client, STREAM_SHUT_RDWR);
101
102 $avg = 0;
103 $min = 0;
104 $max = 0;
105
106 foreach ($data as $cnt) {
107         if ($cnt > $max) {
108                 $max = $cnt;
109         }
110         if (($cnt < $min) || ($min == 0)) {
111                 $min = $cnt;
112         }
113         $avg += $cnt;
114 }
115
116 if (count($data) > 0) {
117         $avg = round($avg / count($data));
118 }
119
120 out(__FILE__, __LINE__, "MIN/AVG/MAX=${min}/${avg}/${max}");
121 out(__FILE__, __LINE__, "INVALID=${invalid}");
122
123 ?>