-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
executable file
·50 lines (35 loc) · 1.08 KB
/
server.php
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
50
<?php
set_time_limit(0);
list($host, $port) = !isset($argv[1]) ? array('0.0.0.0',9900) : explode(':',$argv[1]);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("C'e' qualche altro programma in ascolta su sta porta\n");
echo "LISTEN: $host:$port";
//HTTP AUTH:
//http://www.disi.unige.it/person/ReggioG/RETI01WWW/LEZIONI/Appunti2/Cap2a/Cap2a.html
$flog = fopen('server.php.log','a+');
while(1)
{
$spawn = socket_accept($socket);
echo "DEVICE CONNECTED\n"; //TODO ip o id device
while(1)
{
try {
$input = socket_read($spawn, 1024);
//TODO controllo key auth
echo $input;
flush();
fwrite($flog, $input, strlen($input) );
fflush($flog);
$out = 'OK';
@socket_write($spawn, $out, strlen($out));
} catch (Exception $e) {
echo $e->getMessage();
#break;
}
}
socket_close($spawn);
}
fclose($flog);
socket_close($socket);
?>