0
|
1 <?php |
|
2 /* Copyright 2010 Ivo Smits <Ivo@UCIS.nl>. All rights reserved. |
|
3 Redistribution and use in source and binary forms, with or without modification, are |
|
4 permitted provided that the following conditions are met: |
|
5 |
|
6 1. Redistributions of source code must retain the above copyright notice, this list of |
|
7 conditions and the following disclaimer. |
|
8 |
|
9 2. Redistributions in binary form must reproduce the above copyright notice, this list |
|
10 of conditions and the following disclaimer in the documentation and/or other materials |
|
11 provided with the distribution. |
|
12 |
|
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
14 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
15 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR |
|
16 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
17 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
18 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
|
19 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
20 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
21 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
22 |
|
23 The views and conclusions contained in the software and documentation are those of the |
|
24 authors and should not be interpreted as representing official policies, either expressed |
|
25 or implied, of Ivo Smits.*/ |
|
26 |
|
27 $server = '127.0.0.1:15387'; |
|
28 |
|
29 $socket = stream_socket_client('tcp://'.$server) or die("Could not create socket\n"); |
|
30 $buffer = ''; |
|
31 |
|
32 while (TRUE) { |
|
33 $msg = fread($socket, 1024); |
|
34 if (!strlen($msg)) die("Error: end of file\n"); |
|
35 $buffer .= $msg; |
|
36 while (strlen($buffer) > 2) { |
|
37 $len = ord($buffer[0]) * 256 + ord($buffer[1]); |
|
38 if ($len <= 0 || $len > 1024) die("Error: protocol error\n"); |
|
39 if (strlen($buffer) < 2 + $len) break; |
|
40 $msg = substr($buffer, 2, $len); |
|
41 $buffer = substr($buffer, 2 + $len); |
|
42 printf('Received message: '); |
|
43 $parts = explode("\0", $msg); |
|
44 $ret = array(); |
|
45 for ($i = 0; $i < count($parts)-1; $i += 2) $ret[$parts[$i]] = $parts[$i+1]; |
|
46 print_r($ret); |
|
47 } |
|
48 unset($msg, $len, $parts, $ret); |
|
49 } |