Maxipro

 
<<< Back

WOL-test

magicPacket('A8-F9-4B-24-5F-AF');

function magicPacket($mac,$addr='255.255.255.255',$socket_number=7) {

//split up the mac address based upon the colons in the string
    $addr_byte = explode(':', $mac);
    $hw_addr = '';

for ($a=0; $a &lt;6; $a++) 
  $hw_addr .=chr(hexdec($addr_byte[$a])); //convert the hex to its decimal equivalent, encode as a character, and repeat 16 times

    $msg = str_repeat(chr(255),6); //FF in decimal is 255, which is then encoded as a char as with our mac address
for ($a = 1; $a &lt;= 16; $a++) 
  $msg .= $hw_addr;

    $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);  //create our socket

    if ($s == false) {
      echo &quot;Error creating socket!\n&quot;;
      echo &quot;Error code is '&quot;.socket_last_error($s).&quot;'- &quot; . socket_strerror(socket_last_error($s));
      return false;
    } else {
      // setting a broadcast option to socket:
      //$opt_ret = socket_set_option($s, 1, 6, TRUE);
      $opt_ret = socket_set_option($s,SOL_SOCKET,SO_BROADCAST,true);
      if($opt_ret &lt;0) {
        echo &quot;setsockopt() failed, error: &quot; . strerror($opt_ret) .&quot;\n&quot;;
        return false;
      }
  if(socket_sendto($s, $msg, strlen($msg), 0, $addr,$socket_number)) {
        socket_close($s);
        return true;
      } else {
        return false;
      }
    }
 }

Discuss (0) (0)

Чита, Россия