-
Notifications
You must be signed in to change notification settings - Fork 3
/
Socket.class.php
executable file
·43 lines (36 loc) · 1.28 KB
/
Socket.class.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
<?php
class Socket {
public $socket = null;
private $cloud = array(
'address' => '127.0.0.1',
'port' => '1234'
);
public function udp_send($address, $port, $data, $whoami = 'Cloud') {
if (!$this->socket) {
$socket = stream_socket_client("udp://$address:$port");
fwrite($socket, $data);
$buf = fgets($socket, 128);
print_r($buf);
fclose($socket);
} else {
stream_socket_sendto($this->socket, $whoami . '/' . date('Y-m-d H:i:s') . '/' . $peer . "\r\n", 0, $peer);
echo '> SENT MSG!' . "\n";
}
}
public function udp_server($address, $port, $whoami = 'Cloud') {
$this->socket = stream_socket_server("udp://" . $address . ":" . $port, $errno, $errstr, STREAM_SERVER_BIND);
if (!$this->socket) {
die("$errstr ($errno)");
}
if ($whoami != 'Cloud') {
stream_socket_sendto($this->socket, $whoami . '/' . date('Y-m-d H:i:s') . "\r\n", 0, $this->cloud['address'] . ':' . $this->cloud['port']);
echo "> MSG CLOUD SENT!\n";
}
do {
$packet = stream_socket_recvfrom($this->socket, 128, 0, $peer);
echo "$peer -- " . $packet . "\n";
stream_socket_sendto($this->socket, $whoami . '/' . date('Y-m-d H:i:s') . '/' . $peer . "\r\n", 0, $peer);
echo '> REPLY SENT!' . "\n";
} while ($packet !== false);
}
}