-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiscoveryService.php
executable file
·79 lines (73 loc) · 2.48 KB
/
DiscoveryService.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/php5-cgi -q
<?php
// Set time limit to indefinite execution
set_time_limit(0);
// Set the ip and port we will listen on
$address = '172.31.1.146';
$port = 1900;
$max_clients = 10;
// Array that will hold client information
$clients = Array();
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
echo "Client started";
// Loop continuously
while (true) {
// Setup clients listen socket for reading
$read[0] = $sock;
for ($i = 0; $i < $max_clients; $i++) {
if ($client[$i]['sock'] != null)
$read[$i + 1] = $client[$i]['sock'];
}
// Set up a blocking call to socket_select()
$ready = socket_select($read,$write=null,$except=null,null);
/* if a new connection is being made add it to the client array */
echo "Client connected";
if (in_array($sock, $read)) {
for ($i = 0; $i < $max_clients; $i++) {
if ($client[$i]['sock'] == null) {
$client[$i]['sock'] = socket_accept($sock);
break;
} elseif ($i == $max_clients - 1)
print ("too many clients");
}
if (--$ready <= 0)
continue;
} // end if in_array
// If a client is trying to write - handle it now
for ($i = 0; $i < $max_clients; $i++) { // for each client
if (in_array($client[$i]['sock'], $read)) {
$input = socket_read($client[$i]['sock'], 1024);
if ($input == null) {
// Zero length string meaning disconnected
unset($client[$i]);
}
$n = trim($input);
if ($input == 'exit') {
echo "Closing Connection";
// requested disconnect
socket_close($client[$i]['sock']);
} elseif ($input) {
// strip white spaces and write back to user
//$output = ereg_replace("[ \t\n\r]", "", $input) . chr(0);
$output = "Message Received";
socket_write($client[$i]['sock'], $output);
}
print($input);
} else {
// Close the socket
//socket_close($client[$i]['sock']);
socket_close($sock);
unset($client[$i]);
}
}
echo "HERE";
} // end while
// Close the master sockets
socket_close($sock);
echo "CLOSED";
?>