(PHP 4 >= 4.1.0, PHP 5)
socket_send — Sendet Daten an einen verbundenen Socket
Die Funktion socket_send() sendet len Bytes aus dem Puffer buf an den Socket socket.
Ein gültiger Socket-Deskriptor, der von socket_create() oder socket_accept() erzeugt wurde.
Ein Puffer, der die Daten enthält, die an den entfernten Host gesendet werden.
Die Anzahl Bytes, die aus dem Puffer buf an den entfernten Host gesendet werden.
Der Parameter flags kann beliebige Kombinationen der folgenden Flags enthalten, die mit einem binären OR (|) verknüpft werden.
MSG_OOB | Sende OOB-Daten (out-of-band). |
MSG_EOR | Setze eine Ende-Marke für den Datensatz. Die gesendeten Daten beenden die Übertragung. |
MSG_EOF | Schließe den Socket auf der Senderseite und füge eine entsprechende Benachrichtigung an das Ende der gesendeten Daten an. Die gesendeten Daten beenden die Übertragung. |
MSG_DONTROUTE | Umgehe das Routing, sende direkt ber die Schnittstelle. |
socket_send() gibt die Anzahl der gesendeten Bytes zurück oder FALSE, falls ein Fehler auftrat.
In php-5.2.0, there's no MSG_EOF anymore. This is not even needed if you make sure you handle writes and close of sockets correctly.
I changed the Magic Packet function to this. I beleave that an function may not return any value, only an true/false (in the most way's).
<?PHP
function wol_magic_packet($mac,$addr='255.255.255.255') {
//Requirements__________________________
// You need to load the php_sockets.dll (in case of Windows, don't
// know @ linux, compile with --socket-support i beleave ).
// Otherwise he cannot find the socket_create function
//Usage________________________________
// $addr:
// You will send and broadcast tho this addres.
// Normaly you need to use the 255.255.255.255 adres, so i made it as default. So you don't need
// to do anything with this.
// $mac:
// You will WAKE-UP this WOL-enabled computer, you need to add the MAC-addres here.
//
//Return________________________________
// TRUE: When socked was created succesvolly and the message has been send.
// FALSE: Something went wrong
//
//Example_1_____________________________
// When the message has been send you will see the message "Done...."
//
// if ( wol_magic_packet ( '00:00:00:00:00:00' ) )
// echo 'Done...';
// else
// echo 'Error while sending';
//
//Example_2_____________________________
// To loop more computers:
//
// $arr = array(
// '00:00:00:00:00:00',
// '00:00:00:00:00:00'
// );
// foreach($arr as $this_id => $this_mac)
// if (! wol_magic_packet ( $this_mac ))
// echo 'Error while sending to ['. $this_mac .']<br />'."\r\n";
//___________________________________________
//Check if it's an real MAC-addres and split it into an array
if (!preg_match("/([A-F0-9]{2}[-:]){5}[A-F0-9]{2}/",$mac,$maccheck))
return false;
$addr_byte = preg_split("/[-:]/",$maccheck[0]);
//Creating hardware adress
$hw_addr = '';
for ($a=0; $a < 6; $a++)//Changing mac adres from HEXEDECIMAL to DECIMAL
$hw_addr .= chr(hexdec($addr_byte[$a]));
//Create package data
$msg = str_repeat(chr(255),6);
for ($a = 1; $a <= 16; $a++)
$msg .= $hw_addr;
//Sending data
if ( //If
function_exists('socket_create') AND //socket_create exists
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) AND //Can create the socket
$sock_data = socket_connect($sock, $addr, 2050) //Can connect to the socket
) { //Then
$sock_data = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); //Set
$sock_data = socket_write($sock, $msg, strlen($msg)); //Send data
socket_close($sock); //Close socket
return true;
} else //Esle? :P
return false;
}
?>