Wake-on-LAN from a .NET Micro Framework Device

Michael Schwarz on Sunday, March 9, 2008

Well, I love the easy development of embedded devices with the .NET Micro Framework. As the device I'm currently using is really small I tried to build a small Web site on it to start my private servers at home using Wake-on-LAN [1]. You'll find a lot of helper methods samples on the Internet but I couldn't find on source code that was working on the .NET Micro Framework. The problem is that there are too many missing methods or enum values.

Here is the source code I'm using now which is working.

<pre style="background-color:White;;overflow: auto;"><div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> WakeUp(</span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[] mac) { </span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (Socket socket </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { IPEndPoint endPoint </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> IPEndPoint(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> IPAddress(</span><span style="color: #800080;">4294967295</span><span style="color: #000000;">), </span><span style="color: #800080;">40000</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> IP 255.255.255.255</span><span style="color: #008000;"> </span><span style="color: #000000;"> socket.Connect(endPoint);

</span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[] packet </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> </span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[</span><span style="color: #800080;">17</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">6</span><span style="color: #000000;">]; </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #800080;">6</span><span style="color: #000000;">; i</span><span style="color: #000000;">++</span><span style="color: #000000;">) packet[i] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0xFF</span><span style="color: #000000;">;

</span><span style="color: #0000FF;">for</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">; i </span><span style="color: #000000;">&lt;=</span><span style="color: #000000;"> </span><span style="color: #800080;">16</span><span style="color: #000000;">; i</span><span style="color: #000000;">++</span><span style="color: #000000;">) </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> j </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; j </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #800080;">6</span><span style="color: #000000;">; j</span><span style="color: #000000;">++</span><span style="color: #000000;">) packet[i </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">6</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> j] </span><span style="color: #000000;">=</span><span style="color: #000000;"> mac[j];

socket.Send(packet); } }</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com -->

Using my Web server [2] (which I will publish this week at http://www.codeplex.com/ajaxnetmicro [3]) including the Ajax.NET M! library [4] it is really cool to connect to the .NET Micro Framework enabled device from outside and start your servers as you need.