Does Pascal code turn you on?

Computing

It can if you want it to. The Synapse socket library for Pascal makes it quite easy to create a Wake On LAN program, which can be used to turn your computers on over the network. By sending a special packet via UDP, you can turn on any modern computer remotely, as long as it is plugged into an Ethernet network. This short article will show you how to write a program to send a Wake On LAN packet using Pascal.

Note: Before you start this program, make sure you have downloaded Synapse and placed it where FreePascal can find it. Also add the following to the top of your program: uses StrUtils, SysUtils, blcksock;

The Packet

The “Magic Packet” consists of six FF bytes, followed by the MAC (hardware) address of the computer, repeated 16 times. To do this first we loop through the string containing the address, then use StrToInt plus a dollar sign to convert the hexadecimal number to a decimal. Then we pass that to the Chr function, which gives the actual byte. After the address string is generated, we use DupeString to repeat it 16 times.

function BuildWOLPacket(wolAddress : string) : string;
var
	i : integer;
	addressByte, address : string;
begin
	// This function will work with a MAC address formatted like this:
	// "01-23-45-67-89-0A" ie. delimited by a character.
	// So if the address isn't 17 chars long, we will quit.
	if length(wolAddress) <> 17 then exit;
 
	for i := 0 to 5 do
	begin
		addressByte := UpperCase(MidStr(wolAddress, 3*i+1,2));
		address := address + Chr(StrToInt('$' + addressByte));
	end;
 
	// Repeat #255 6 times, then repeat the address 16 times.
	result := DupeString(#255,6) + DupeString(address,16);
end;

This function will return a string which contains a WoL “Magic Packet.”

Sending the packet

To send this packet, we need to use the Synapse socket library. The Synapse library has a component called TUDPBlockSocket which we will use to send the packet to the broadcast address (255.255.255.255) on port 0 (some other ports can be used, eg. 9 and 7, however 0 seems to work).

procedure SendWOLPacket(wolPacket : string);
var
	sock : TUDPBlockSocket;
begin
	// Create the socket
	sock := TUDPBlockSocket.Create();
 
	// Enable broadcast support (this is important)
	sock.EnableBroadcast(True);
 
	sock.Connect('255.255.255.255','0')
 
	// Send the string.
	sock.SendString(wolPacket);
 
	// Close the socket.
	sock.CloseSocket();
end;

Now that you have these two functions, it is easy to wake up a computer:

wolPacket := BuildWOLPacket('00:13:37:BA:DA:55');
SendWOLPacket(wolPacket);

If it doesn’t work…

You might want to try changing the port number from 0 to 9 or 7. There doesn’t seem to be a standard for which port is used for Wake On LAN (it actually doesn’t really matter on a LAN).

Also since this uses the Delphi syntax you will need to compile with the -Sd option if you’re using the FreePascal compiler.

–Ben

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Slashdot
  • StumbleUpon
  • TwitThis
  • Print this article!
  • Reddit
Explore posts in the same categories: Computing

Comment: