RSSAminetKontakt

Home/Seiten/server_udp.php

server_udp.php, Sonntag, 12. November 2017 16:18
<?php
class udpsocket {
	public function __construct($ip,$port,$parse) {
		$this->ip = $ip;
		$this->port = $port;
		$this->func = $parse;
		$this->open();
	}
	public function parse($sk, $buf, $from, $port) {
		call_user_func($this->func, $sk, $buf, $from, $port);
	}
	public function close() {
		socket_close($this->socket);
	}
	public function open() {
		$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
		if(is_resource($this->socket)) {
			socket_set_option($this->socket,SOL_SOCKET,SO_BROADCAST,true);
			socket_set_option($this->socket,SOL_SOCKET,SO_REUSEPORT,true);
			socket_bind($this->socket, $this->ip, $this->port);
		}
	}
	public function select() {
		$read   = array($this->socket);
		$i  = NULL;
		$num_changed_sockets = socket_select($read, $i, $i, 1);
		if ($num_changed_sockets > 0) {
			foreach($read as $sk) {
				socket_recvfrom($sk, $buf, 8192, MSG_DONTWAIT, $from, $port);
				$this->parse($sk, $buf, $from, $port);
			}
		}
	}
	public function send($msg, $from, $port) {
		socket_sendto($this->socket, $msg, strlen($msg), 0, $from, $port);
	}
}
?>
Erstellt von: Robin Hüskes
Erstellt am: Sonntag, 12. November 2017 16:18
Geändert von: Robin Hüskes
Geändert am: Sonntag, 12. November 2017 16:19
Zum Live Linux: