note 26941 rejected from function.socket-send by nicobn
| From: | nicobn@php.net | Date: | Mon, 25 Jun 2007 17:50:41 +0000 |
| Subject: | note 26941 rejected from function.socket-send by nicobn | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127960@lists.php.net to get a copy of this message | ||
Note Submitter: hotkey at spr dot at
----
Hi guys!
This is my example for socket_create, _connect and _send: It is a Wake On Lan (WOL) function to
power-up a Computer with given MAC-Adress.
greetz!
//HotKey
- - - - - - - - - >8 - - - cut here! - - - - - -
function WakeOnLan($addr, $mac) { # (C)opyright 2K2 hotkey@spr.at
$addr_byte = explode(':', $mac);
$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
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false) {
echo "Error creating socket!\n";
echo "Error:".socket_strerror(socket_last_error($s));
} else {
$e = socket_connect($s, $addr, 2050);
if ($e == false) {
echo "connection failed\n";
echo "Error:".socket_strerror(socket_last_error($s));
} else {
$e = socket_setopt($s, SOL_SOCKET, SO_BROADCAST, 1);
$e = socket_write($s, $msg, strlen($msg));
socket_close($s);
echo "Magic Packet sent (".$e.") to ".$addr.", MAC=".$mac;
}
}
}
WakeOnLan('255.255.255.255', 'MAC:MAC:MAC:MAC:MAC:MAC'); #put the
MAC-to-be-woken here!
# IP must be 255.255.255.255 - at least it didnt work otherwise...