0

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?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Ghost_Dz
  • 9
  • 2
  • This site does the trick online: https://www.depicus.com/wake-on-lan/woli – Ghost_Dz Jul 26 '22 at 15:56
  • 6
    This is not really a programming question, or a Delphi question, or a FireMonkey question. You need to understand what the code is actually doing. You are sending to `255.255.255.255` which is the [broadcast address](https://en.wikipedia.org/wiki/Broadcast_address) for the local network. Your work computer is not on your local network, so it doesn't receive the packet. Your issue is not with your code, your issue is that you need to work out where to send this message. My expectation is that your office network won't accept broadcast packets from raw internet – David Heffernan Jul 26 '22 at 16:09
  • 3
    And that's even if your office network has a public IP address at all. And if it does have a public IP address it's likely to be very locked down. So talk to your office IT people and let them help you. Of course, if you had a VPN then your approach might have more chance of working, because your computer would be attached to the office network. But even then there may be issues. – David Heffernan Jul 26 '22 at 16:10
  • If So, So how this app works: https://www.depicus.com/wake-on-lan/wake-on-lan-gui – Ghost_Dz Jul 29 '22 at 11:19
  • What about that app contradicts what I said? – David Heffernan Jul 29 '22 at 17:48

0 Answers0