本文整理汇总了PHP中socket_recvfrom函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_recvfrom函数的具体用法?PHP socket_recvfrom怎么用?PHP socket_recvfrom使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_recvfrom函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_socket_server
public static function create_socket_server($host = "0.0.0.0", $port = 3465)
{
//Create a UDP socket
if (!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [{$errorcode}] {$errormsg} \n");
}
echo "Socket created \n";
// Bind the source address
if (!socket_bind($sock, $host, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [{$errorcode}] {$errormsg} \n");
}
echo "Socket bind OK \n";
// Connect to Redis.
$redis = new \Predis\Client();
// Do some communication, this loop can handle multiple clients
while (true) {
//Receive some data
$r = socket_recvfrom($sock, $buf, Eventsd::MaxUDPPacket, 0, $remote_ip, $remote_port);
$redis->rpush('events_que', $buf);
unset($event, $buf, $remote_ip, $remote_port);
}
socket_close($sock);
}
示例2: scan
/**
* scans the local network for upnp devices
*
* @return array with discovered devices
*/
function scan()
{
$this->checkCredentials();
$result = array();
$buffer = '';
$tmp = '';
$headers = "M-SEARCH * HTTP/1.1\r\nHost:239.255.255.250:1900\r\nST:" . $this->type . "\r\nMan:\"ssdp:discover\"\r\nMX:3\r\n\r\n";
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 10000));
$i = 0;
socket_sendto($socket, $headers, 1024, 0, '239.255.255.250', 1900);
while (@socket_recvfrom($socket, $buffer, 1024, MSG_WAITALL, $tmp, $mp)) {
$i++;
$res = $this->decode($buffer);
if (isset($res['location'])) {
$res_details = $this->getDetails($res['location']);
if ($res_details[0] == true) {
$res['details'] = $res_details[1];
} else {
$res['details'] = false;
}
}
$result[] = $res;
}
socket_close($socket);
return array(true, $result);
}
示例3: mServer
public function mServer()
{
$sock = socket_create(AF_INET, SOCK_DGRAM, getprotobyname('udp'));
$mIP = '239.255.255.250';
if (!socket_bind($sock, $mIP, 1900)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [{$errorcode}] {$errormsg} \n");
}
socket_set_option($sock, IPPROTO_IP, MCAST_JOIN_GROUP, array("group" => '239.255.255.250', "interface" => 0));
while (1) {
echo "Waiting for data ... \n";
socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "{$remote_ip} : {$remote_port} -- " . $buf;
$query = $this->parseHeaders($buf);
if (!isset($query["m-search"])) {
continue;
}
$response = "HTTP/1.1 200 OK\r\n";
$response .= "CACHE-CONTROL: max-age=1810\r\n";
$response .= "DATE: " . date("r") . "\r\n";
$response .= "EXT:\r\n";
$response .= "LOCATION: http://192.168.7.123:9000/TMSDeviceDescription.xml\r\n";
$response .= "SERVER: Linux/3.x, UPnP/1.1, fheME/0.6\r\n";
$response .= "ST: urn:fheme.de:service:X_FIT_HomeAutomation:1\r\n";
$response .= "USN: uuid:f6da16ab-0d1b-fe1c-abca-82aacf4afcac::urn:fheme.de:service:X_FIT_HomeAutomation:1\r\n";
$response .= "Content-Length: 0\r\n";
$response .= "\r\n";
//Send back the data to the client
socket_sendto($sock, $response, strlen($response), 0, $mIP, $remote_port);
}
socket_close($sock);
}
示例4: get_peers_blocking
private function get_peers_blocking($info_hash, $host = "router.bittorrent.com", $port = 6881)
{
//create a UDP socket to send commands through
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
//Create Command Packet
$packet = bencode::encode(array("id" => $this->get_unique_node_id(), "info_hash" => hex2bin($info_hash)), array("q" => "get_peers", "t" => $this->unique_id(), "y" => "q"));
socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);
//set timeout
$timeout = array('sec' => 5, 'usec' => 0);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);
$time = time();
//recieve data
try {
socket_recvfrom($socket, $buf, 12000, 0, $host, $port);
} catch (Exception $e) {
echo "Error";
return FALSE;
}
//have to manually do the timeout, cant seem to get info from this socket
if (time() - $time >= 4) {
socket_close($socket);
return FALSE;
}
//close socket so bad shit don't happen
socket_close($socket);
return nodeExtract::return_nodes(bencode::decode($buf));
}
示例5: udp
public function udp()
{
set_time_limit(0);
$ip = '127.0.0.1';
$port = 9527;
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, $ip, $port);
$mem = init_mem();
if ($mem == false) {
return;
}
while (true) {
socket_recvfrom($socket, $content, 1024, 0, $ip, $port);
if (empty($content)) {
continue;
}
//$this->log_data($content);
$minute = (int) date("i");
$datagram = $mem->get('datagram');
if (empty($datagram)) {
$datagram = array();
}
$datagram[$minute][] = $content;
$mem->set('datagram', $datagram);
}
socket_close($socket);
$mem->close();
}
示例6: read
public function read(&$buf, &$source, &$port)
{
if ($this->connected === false) {
return false;
}
return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port);
}
示例7: search
public function search($st = 'ssdp:all', $mx = 2, $man = 'ssdp:discover', $from = null, $port = null, $sockTimout = '2')
{
$request = 'M-SEARCH * HTTP/1.1' . "\r\n";
$request .= 'HOST: 239.255.255.250:1900' . "\r\n";
$request .= 'MAN: "' . $man . '"' . "\r\n";
$request .= 'MX: ' . $mx . '' . "\r\n";
$request .= 'ST: ' . $st . '' . "\r\n";
$request .= 'USER-AGENT: ' . $this->user_agent . "\r\n";
$request .= "\r\n";
$socket = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, true);
socket_sendto($socket, $request, strlen($request), 0, '239.255.255.250', 1900);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockTimout, 'usec' => '0'));
$response = array();
do {
$buf = null;
socket_recvfrom($socket, $buf, 1024, MSG_WAITALL, $from, $port);
if (!is_null($buf)) {
$data = $this->parseSearchResponse($buf);
$response[$data['usn']] = $data;
}
} while (!is_null($buf));
socket_close($socket);
return $response;
}
示例8: tftp_fetch
/**
* Does a TFTP Check by connecting to $host looking for $filename
* @author http://www.php.net/manual/en/function.socket-create.php#43057
* @param string $host
* @param string $filename
* @return mixed file contents
*/
function tftp_fetch($host, $filename)
{
//first off let's check if this is installed or disabled
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// create the request packet
$packet = chr(0) . chr(1) . $filename . chr(0) . 'octet' . chr(0);
// UDP is connectionless, so we just send on it.
socket_sendto($socket, $packet, strlen($packet), 0x100, $host, 69);
$buffer = '';
$port = '';
$ret = '';
$time = time();
do {
$new_time = time() - $time;
if ($new_time > 5) {
break;
}
// $buffer and $port both come back with information for the ack
// 516 = 4 bytes for the header + 512 bytes of data
socket_recvfrom($socket, $buffer, 516, 0, $host, $port);
// add the block number from the data packet to the ack packet
$packet = chr(0) . chr(4) . substr($buffer, 2, 2);
// send ack
socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);
// append the data to the return variable
// for large files this function should take a file handle as an arg
$ret .= substr($buffer, 4);
} while (strlen($buffer) == 516);
// the first non-full packet is the last.
return $ret;
}
示例9: mSearch
/**
* Perform an M-SEARCH multicast request for detecting UPnP-devices in network.
*
* @todo Allow unicasting.
* @todo Sort arguments better.
*/
public function mSearch($st = 'ssdp:all', $mx = 2, $man = 'ssdp:discover', $from = null, $port = null, $sockTimout = '5')
{
// BUILD MESSAGE
$msg = 'M-SEARCH * HTTP/1.1' . "\r\n";
$msg .= 'HOST: 239.255.255.250:1900' . "\r\n";
$msg .= 'MAN: "' . $man . '"' . "\r\n";
$msg .= 'MX: ' . $mx . "\r\n";
$msg .= 'ST:' . $st . "\r\n";
$msg .= 'USER-AGENT: ' . static::USER_AGENT . "\r\n";
$msg .= '' . "\r\n";
// MULTICAST MESSAGE
$sock = socket_create(AF_INET, SOCK_DGRAM, 0);
$opt_ret = socket_set_option($sock, 1, 6, TRUE);
$send_ret = socket_sendto($sock, $msg, strlen($msg), 0, '239.255.255.250', 1900);
// SET TIMEOUT FOR RECIEVE
socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockTimout, 'usec' => '0'));
// RECIEVE RESPONSE
$response = array();
do {
$buf = null;
@socket_recvfrom($sock, $buf, 1024, MSG_WAITALL, $from, $port);
if (!is_null($buf)) {
$response[] = $this->parseMSearchResponse($buf);
}
} while (!is_null($buf));
// CLOSE SOCKET
socket_close($sock);
return $response;
}
示例10: doTick
public function doTick()
{
$this->lastmeasure = microtime(true);
$timeout = 50000;
while (!$this->shutdown and $timeout >= 0) {
$address = $port = $buffer = null;
if (@socket_recvfrom($this->socket, $buffer, 65535, 0, $address, $port) > 0) {
isset($this->banlist[(string) $address]) ? $this->loginfo((int) $this->banlist[$address] . ' ' . time()) : false;
if (!isset($this->banlist[$address]) or (int) $this->banlist[$address] < time()) {
switch ($buffer[0]) {
case Info::PKHEAD_DATA:
$this->pushMainQueue(new DataPacket($address, $port, $buffer));
break;
case Info::PKHEAD_BASE64:
$this->pushMainQueue(new Base64Packet($address, $port, $buffer));
break;
case Info::PKHEAD_ENUM:
$this->pushMainQueue(new EnumPacket($address, $port, $buffer));
break;
default:
$this->pushMainQueue($pk - new InvalidPacket($address, $port, $buffer));
$this->logInfo("Custompacket received packet with invalid header! Printing dump...");
$pk->printDump();
}
}
}
$timeout = $this->lastmeasure - microtime(true);
}
}
示例11: _read
private function _read($sock, $len, $timeout)
{
$data = '';
$size = 0;
while ($size < $len) {
$r = array($sock);
$w = array();
$e = array();
$n = socket_select($r, $w, $e, $timeout);
if ($n === false) {
throw new Bert_Rpc_Error_ConnectionError($this->_svc->host, $this->_svc->port, socket_strerror(socket_last_error()));
} elseif ($n === 0) {
throw new Bert_Rpc_Error_ReadTimeoutError($this->_svc->host, $this->_svc->port, $this->_svc->timeout);
}
$bytes = socket_recvfrom($sock, $msg, $len - $size, 0, $name, $port);
if ($bytes === false) {
throw new Bert_Rpc_Error_ConnectionError($this->_svc->host, $this->_svc->port, socket_strerror(socket_last_error()));
} elseif ($bytes === 0) {
throw new Bert_Rpc_Error_ReadError($this->_svc->host, $this->_svc->port);
}
$size += $bytes;
$data .= $msg;
}
return $data;
}
示例12: time
/**
Send ICMP echo request to specified host; Return array containing
minimum/average/maximum round-trip time (in millisecs) and number of
packets received, or FALSE if host is unreachable
@return mixed
@param $addr string
@param $dns boolean
@param $count integer
@param $wait integer
@param $ttl integer
@public
**/
static function ping($addr, $dns = FALSE, $count = 3, $wait = 3, $ttl = 30)
{
// ICMP transmit socket
$tsocket = socket_create(AF_INET, SOCK_RAW, 1);
// Set TTL
socket_set_option($tsocket, 0, PHP_OS != 'Linux' ? 4 : 2, $ttl);
// ICMP receive socket
$rsocket = socket_create(AF_INET, SOCK_RAW, 1);
// Bind to all network interfaces
socket_bind($rsocket, 0, 0);
// Initialize counters
list($rtt, $rcv, $min, $max) = array(0, 0, 0, 0);
for ($i = 0; $i < $count; $i++) {
// Send ICMP header and payload
$data = uniqid();
$payload = self::hexbin('0800000000000000') . $data;
// Recalculate ICMP checksum
if (strlen($payload) % 2) {
$payload .= self::hexbin('00');
}
$bits = unpack('n*', $payload);
$sum = array_sum($bits);
while ($sum >> 16) {
$sum = ($sum >> 16) + ($sum & 0xffff);
}
$payload = self::hexbin('0800') . pack('n*', ~$sum) . self::hexbin('00000000') . $data;
// Transmit ICMP packet
@socket_sendto($tsocket, $payload, strlen($payload), 0, $addr, 0);
// Start timer
$time = microtime(TRUE);
$rset = array($rsocket);
$tset = NULL;
$xset = NULL;
// Wait for incoming ICMP packet
socket_select($rset, $tset, $xset, $wait);
if ($rset && @socket_recvfrom($rsocket, $reply, 255, 0, $host, $port)) {
$elapsed = 1000.0 * (microtime(TRUE) - $time);
// Socket didn't timeout; Record round-trip time
$rtt += $elapsed;
if ($elapsed > $max) {
$max = $elapsed;
}
if (!($min > 0) || $elapsed < $min) {
$min = $elapsed;
}
// Count packets received
$rcv++;
if ($host) {
$addr = $host;
}
}
}
socket_close($tsocket);
socket_close($rsocket);
return $rcv ? array('host' => $dns ? gethostbyaddr($addr) : $addr, 'min' => (int) round($min), 'max' => (int) round($max), 'avg' => (int) round($rtt / $rcv), 'packets' => $rcv) : FALSE;
}
示例13: __construct
public function __construct()
{
// Send our packet and receive whatever the server has to send back to us.
if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
socket_sendto($socket, $this->packet, strlen($this->packet), 0, $this->ip, $this->port);
$r = socket_recvfrom($socket, $buf, $this->bufferSize, 0, $this->ip, $this->port);
// Remove first 23 bytes, we don't need it.
$this->buffer = array_slice(str_split(bin2hex($buf), 2), 23);
}
}
示例14: recvfrom
public function recvfrom(&$ip)
{
$int = @socket_recvfrom($this->socket, $line, 1500, 0, $from, $port);
if ($int) {
$ip = $from . ":" . $port;
return $line;
} else {
usleep(1000);
}
}
示例15: listen
function listen($callback, $max_packets = 0, $message_types = array())
{
set_time_limit(0);
$current_packet = 1;
while (socket_recvfrom($this->socket, $buffer, 4096, 0, $host, $port) && (!$max_packets || $current_packet <= $max_packets)) {
$result = $this->decodePacket($buffer, $host, $port);
if (!$message_types || in_array($result['message']['type'], $message_types)) {
call_user_func($callback, $result);
$current_packet++;
}
}
}