本文整理汇总了PHP中socket_sendto函数的典型用法代码示例。如果您正苦于以下问题:PHP socket_sendto函数的具体用法?PHP socket_sendto怎么用?PHP socket_sendto使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了socket_sendto函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: wol
function wol($host, $mac)
{
//get the broadcast addr
$range = $this->iprange($host, 24);
$broadcast = $range['last_ip'];
$mac_array = explode(':', $mac);
$hwaddr = '';
foreach ($mac_array as $octet) {
$hwaddr .= chr(hexdec($octet));
}
// Create Magic Packet
$packet = '';
for ($i = 1; $i <= 6; $i++) {
$packet .= chr(255);
}
for ($i = 1; $i <= 16; $i++) {
$packet .= $hwaddr;
}
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($sock) {
$options = socket_set_option($sock, 1, 6, true);
if ($options >= 0) {
$e = socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 9);
socket_close($sock);
}
}
}
示例2: processPacket
function processPacket($packetData)
{
$packet = new dhcpPacket();
$packet->parse($packetData);
$processor = new dhcpRequestProcessor($this, $this->packetProcessor, $this->storage, $packet);
if ($responsePacket = $processor->getResponse()) {
$responseData = $responsePacket->build();
$this->verbosity && (print "Sending response" . "\n");
$ciaddr = $packet->getClientAddress();
if ($ciaddr == '0.0.0.0') {
$this->verbosity && (print "Switching to broadcast address...\n");
$ciaddr = '255.255.255.255';
}
$this->verbosity && (print "Attempting to send response packet to " . $ciaddr . "\n");
$numBytesSent = socket_sendto($this->socket, $responseData, strlen($responseData), 0, $ciaddr, 68);
if ($numBytesSent === FALSE) {
$this->verbosity && (print "send failed for specific address, broadcast.\n");
$numBytesSent = socket_sendto($this->socket, $responseData, strlen($responseData), 0, "255.255.255.255", 68);
$numBytesSent === FALSE && $this->verbosity && printf('socket send error: %s\\n', socket_strerror(socket_last_error($this->socket)));
}
$numBytesSent && $this->verbosity && (print "Response packet sent.\n");
} else {
$this->verbosity && (print "Packet ignored\n");
}
}
示例3: send
/**
* 送信する
* @param string $buffer
*/
public function send($buffer)
{
$result = socket_sendto($this->resource, $buffer, strlen($buffer), $this->flags, $this->address, $this->port);
if ($result === false) {
throw new \RuntimeException('send fail');
}
}
示例4: logData
public function logData()
{
global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgUDPProfilerFormatString;
$this->close();
if (!function_exists('socket_create')) {
# Sockets are not enabled
return;
}
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$plength = 0;
$packet = "";
foreach ($this->mCollated as $entry => $pfdata) {
if (!isset($pfdata['count']) || !isset($pfdata['cpu']) || !isset($pfdata['cpu_sq']) || !isset($pfdata['real']) || !isset($pfdata['real_sq'])) {
continue;
}
$pfline = sprintf($wgUDPProfilerFormatString, $this->getProfileID(), $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $entry, $pfdata['memory']);
$length = strlen($pfline);
/* printf("<!-- $pfline -->"); */
if ($length + $plength > 1400) {
socket_sendto($sock, $packet, $plength, 0, $wgUDPProfilerHost, $wgUDPProfilerPort);
$packet = "";
$plength = 0;
}
$packet .= $pfline;
$plength += $length;
}
socket_sendto($sock, $packet, $plength, 0x100, $wgUDPProfilerHost, $wgUDPProfilerPort);
}
示例5: flush
/**
* Squirt the metrics over UDP
*
* @param array $data Incoming Data
* @param int $sampleRate the rate (0-1) for sampling.
* @param array|string $tags Key Value array of Tag => Value, or single tag as string
*
* @return null
*/
private function flush($data, $sampleRate = 1, array $tags = null)
{
// sampling
$sampledData = array();
if ($sampleRate < 1) {
foreach ($data as $stat => $value) {
if (mt_rand() / mt_getrandmax() <= $sampleRate) {
$sampledData[$stat] = "{$value}|@{$sampleRate}";
}
}
} else {
$sampledData = $data;
}
if (empty($sampledData)) {
return;
}
foreach ($sampledData as $stat => $value) {
if ($tags !== NULL && is_array($tags) && count($tags) > 0) {
$value .= '|#';
foreach ($tags as $tag_key => $tag_val) {
$value .= $tag_key . ':' . $tag_val . ',';
}
$value = substr($value, 0, -1);
} elseif (isset($tags) && !empty($tags)) {
$value .= '|#' . $tags;
}
$message = "{$stat}:{$value}";
// Non - Blocking UDP I/O - Use IP Addresses!
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_nonblock($socket);
socket_sendto($socket, $message, strlen($message), 0, $this->server, $this->port);
socket_close($socket);
}
}
示例6: wol
function wol($mac, $broadcast)
{
$mac_array = split(':', $mac);
$hwaddr = '';
//Agafem cada octet de la MAC.
foreach ($mac_array as $octet) {
$hwaddr .= chr(hexdec($octet));
}
//Creem el Magic Packet que rebrà la targeta de xarxa solicitada.
$packet = '';
for ($i = 1; $i <= 6; $i++) {
$packet .= chr(255);
}
for ($i = 1; $i <= 16; $i++) {
$packet .= $hwaddr;
}
//Creem el socket.
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($sock) {
$options = socket_set_option($sock, 1, 6, true);
if ($options >= 0) {
$e = socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7);
//L'enviem.
socket_close($sock);
}
}
return "WOL sended: {$mac}";
}
示例7: getFunctionReport
function getFunctionReport()
{
global $wgUDPProfilerHost;
global $wgUDPProfilerPort;
if ($this->mCollated['-total']['real'] < $this->mMinimumTime) {
# Less than minimum, ignore
return;
}
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$plength = 0;
$packet = "";
foreach ($this->mCollated as $entry => $pfdata) {
$pfline = sprintf("%s %s %d %f %f %f %f %s\n", $this->getProfileID(), "-", $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $entry);
$length = strlen($pfline);
/* printf("<!-- $pfline -->"); */
if ($length + $plength > 1400) {
socket_sendto($sock, $packet, $plength, 0, $wgUDPProfilerHost, $wgUDPProfilerPort);
$packet = "";
$plength = 0;
}
$packet .= $pfline;
$plength += $length;
}
socket_sendto($sock, $packet, $plength, 0x100, $wgUDPProfilerHost, $wgUDPProfilerPort);
}
示例8: sendMessage
function sendMessage($udp_message)
{
$message = vsprintf(str_repeat('%c', count($udp_message)), $udp_message);
global $server_ip, $server_port, $delay, $debug, $error_value, $ok_value;
if ($debug) {
echo '<b>remote host/IP:</b> ' . $server_ip . '<br/>';
echo '<b>remote port:</b> ' . $server_port . '<br/>';
echo '<b>UDP message:</b> ';
foreach ($udp_message as $key => $value) {
echo '0x';
printf("%02x\n", $value);
}
echo '<br/>';
echo '<b>result:</b> ';
}
if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port);
sleep($delay);
socket_close($socket);
echo $ok_value;
} else {
echo $error_value;
}
if ($debug) {
echo '<br/>';
echo '---------- debug info ----------<br/><br/>';
}
}
示例9: 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;
}
示例10: 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;
}
示例11: send
/**
* Send data via UDP
*
* @param ProfilerData $data
*/
public function send(ProfilerData $data)
{
list($host, $port) = $this->getEndpoint($data->getEngine());
if (!$host || !$port) {
return;
}
$profile = $data->getProfile();
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$plength = 0;
$packet = "";
foreach ($data->getEntries() as $name => $pfdata) {
foreach (array('cpu', 'cpu_sq', 'real', 'real_sq') as $key) {
if (!isset($pfdata[$key])) {
$pfdata[$key] = -1;
}
}
$pfline = sprintf("%s %s %d %f %f %f %f %s\n", $profile, "-", $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $name);
$length = strlen($pfline);
if ($length + $plength > self::LESS_THAN_MTU) {
socket_sendto($sock, $packet, $plength, 0, $host, $port);
$packet = "";
$plength = 0;
}
$packet .= $pfline;
$plength += $length;
}
socket_sendto($sock, $packet, $plength, 0x100, $host, $port);
}
示例12: WakeOnLan
function WakeOnLan($addr, $mac_address, $socket_number)
{
$addr_byte = explode(':', $mac_address);
$hw_addr = '';
for ($a = 0; $a < 6; $a++) {
$hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255) . chr(255) . chr(255) . chr(255) . chr(255) . chr(255);
for ($a = 1; $a <= 16; $a++) {
$msg .= $hw_addr;
// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
echo "Error creating socket!\n";
echo "Error code is '" . socket_last_error($s) . "' - " . socket_strerror(socket_last_error($s));
return FALSE;
} else {
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if ($opt_ret < 0) {
echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
return FALSE;
}
if (socket_sendto($s, $msg, strlen($msg), 0, $ip_address, $socket_number)) {
echo "Magic Packet sent successfully!";
socket_close($s);
return TRUE;
} else {
echo "Magic packet failed!";
return FALSE;
}
}
}
}
}
示例13: 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;
}
示例14: 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));
}
示例15: logData
public function logData()
{
global $wgUDPProfilerHost, $wgUDPProfilerPort;
$this->close();
if (isset($this->mCollated['-total']) && $this->mCollated['-total']['real'] < $this->mMinimumTime) {
# Less than minimum, ignore
return;
}
if (!MWInit::functionExists('socket_create')) {
# Sockets are not enabled
return;
}
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$plength = 0;
$packet = "";
foreach ($this->mCollated as $entry => $pfdata) {
if (!isset($pfdata['count']) || !isset($pfdata['cpu']) || !isset($pfdata['cpu_sq']) || !isset($pfdata['real']) || !isset($pfdata['real_sq'])) {
continue;
}
$pfline = sprintf("%s %s %d %f %f %f %f %s\n", $this->getProfileID(), "-", $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $entry);
$length = strlen($pfline);
/* printf("<!-- $pfline -->"); */
if ($length + $plength > 1400) {
socket_sendto($sock, $packet, $plength, 0, $wgUDPProfilerHost, $wgUDPProfilerPort);
$packet = "";
$plength = 0;
}
$packet .= $pfline;
$plength += $length;
}
socket_sendto($sock, $packet, $plength, 0x100, $wgUDPProfilerHost, $wgUDPProfilerPort);
}