-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_socket.php
More file actions
49 lines (43 loc) · 1.25 KB
/
Copy pathio_socket.php
File metadata and controls
49 lines (43 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
stream_set_blocking($socket, 0);
$connections = [];
$read = [];
$write = null;
$except = null;
$buf = '';
while (1) {
if($c = @stream_socket_accept($socket, empty($connections) ? -1 :0,$peer)){
echo $peer.'connected'.PHP_EOL;
fwrite($c, 'hello'.$peer.PHP_EOL);
$connections[$peer] = $c;
}
$read = $connections;
if (stream_select($read, $write, $except, 5)) {
foreach ($read as $c) {
$peer = stream_socket_get_name($c, true);
if(feof($c)){
echo 'conncetion closed'.$peer.PHP_EOL;
fclose($c);
unset($connections[$peer]);
}else{
// $len = fread($c, 4);
// $contents = fread($c, unpack('N', $len)[1]);
$contents = '';
$str = fread($c, 4);
$buf .= $str;
if(($index = stripos($buf, 'qu')) !== false){
$contents = substr($buf, 0,$index);
$buf = substr($buf, $index + 2);
echo $peer.':'.trim($contents).PHP_EOL;
fwrite($c, $contents);
}
}
}
}
}
fclose($socket);
}