Waking up PC Using Wake-On-Lan Over Internet?

505 Views Asked by At

I'm trying to send a Wake-On-Lan packet to a PC in my Office using an application on my Mobile device at home.

This code works for me when I'm connected to the Office's network:

procedure WakeOnLan(const AMacAddress: string); 
type
  TMacAddress = array [1..6] of Byte;  
  TWakeRecord = packed record
    Waker : TMACAddress;
    MAC   : array [0..15] of TMacAddress;
  end;

var
  I          : Integer;
  WR         : TWakeRecord;
  MacAddress : TMacAddress;
  UDPClient  : TIdUDPClient;
  sData      : string;  
begin
  FillChar(MacAddress, SizeOf(TMacAddress), 0);

  sData := Trim(AMacAddress);

  if Length(sData) = 17 then begin

    for I := 1 to 6 do begin
      MacAddress[I] := StrToIntDef('$' + Copy(sData, 1, 2), 0);
      sData := Copy(sData, 4, 17);
    end;

  end;

  for I := 1 to 6  do WR.Waker[I] := $FF;
  for I := 0 to 15 do WR.MAC[I]   := MacAddress;

  UDPClient := TIdUDPClient.Create(nil);
  try
    UDPClient.Host := '255.255.255.255';
    UDPClient.Port := 32767;
    UDPClient.BroadCastEnabled := True;
    UDPClient.Broadcast(RawToBytes(WR, SizeOf(TWakeRecord)), 7);
    UDPClient.SendBuffer(RawToBytes(WR, SizeOf(TWakeRecord)));
    UDPClient.BroadcastEnabled := False;
  finally
    UDPClient.Free;
  end;
end;

But, if I'm connected to my Home network, the code does not work.

How do I connect to the Office Modem/PC before sending the Magic Packets? Do I need to use TSocket instead of TIdUDPClient?

0

There are 0 best solutions below