-
Nov 18th, 2009, 10:16 AM
#1
Thread Starter
Lively Member
WakeOnLAN - How to send packet?
Hi!
Ive been searching around for hours for answers, but I haven't managed to figure it out.
I want to create a simple Windows Mobile (compact framework) application that sends a wakeonlan packet to a specified MAC address. Does anyone have a working sample or something that they would want to share?
Kind Regards
-
Nov 18th, 2009, 10:31 AM
#2
Re: WakeOnLAN - How to send packet?
Here's an example I found on how to do it with standard .NET. Don't know if it'll work in the Compact Framework
-
Nov 18th, 2009, 10:36 AM
#3
Thread Starter
Lively Member
Re: WakeOnLAN - How to send packet?
Thanks for the reply. Ive been there and tried it out a bit but without luck. I did stumble over this C# code though which is supposed to work in CF.
Code:
/// <summary>
/// Wakes a remote PC
/// </summary>
/// <param name="targetMAC">MAC address of target. Must be 6 bytes and MUST be in network order (reversed)</param>
/// <param name="password">Optional password. Must be null or 4 or 6 bytes.</param>
public static void WOL(byte[] targetMAC, byte[] password)
{
// target mac must be 6-bytes!
if (targetMAC.Length != 6)
{
throw new ArgumentException();
}
// check password
if((password != null) &&
(password.Length != 4) &&
(password.Length != 6))
{
throw new ArgumentException();
}
int packetLength = 6 + (20 * 6);
if (password != null)
{
packetLength += password.Length;
}
byte[] magicPacket = new byte[packetLength];
// has a 6-byte header of 0xFF
byte[] header = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Buffer.BlockCopy(header, 0, magicPacket, 0, header.Length);
// repeat the destination MAC 16 times
// your MAC *is* in network (reverse) order, right??
int offset = 6;
for(int i = 0 ; i < 16 ; i++)
{
Buffer.BlockCopy(targetMAC, 0, magicPacket, offset, targetMAC.Length);
offset += 6;
}
if (password != null)
{
Buffer.BlockCopy(password, 0, magicPacket, offset, password.Length);
}
IPEndPoint ep = new IPEndPoint(IPAddress.Broadcast, 9);
UdpClient c = new UdpClient();
c.Send(magicPacket, magicPacket.Length, ep);
}
Is it difficult to convert to vb.net?
-
Nov 18th, 2009, 10:43 AM
#4
Thread Starter
Lively Member
Re: WakeOnLAN - How to send packet?
Code:
Public Shared Sub WOL(ByVal targetMAC() As Byte, ByVal password() As Byte)
' target mac must be 6-bytes!
If (targetMAC.Length <> 6) Then
Throw New ArgumentException
End If
' check password
If ((Not (password) Is Nothing) _
AndAlso ((password.Length <> 4) _
AndAlso (password.Length <> 6))) Then
Throw New ArgumentException
End If
Dim packetLength As Integer = (6 + (20 * 6))
If (Not (password) Is Nothing) Then
packetLength = (packetLength + password.Length)
End If
Dim magicPacket() As Byte = New Byte((packetLength) - 1) {}
' has a 6-byte header of 0xFF
Dim header() As Byte = New Byte() {255, 255, 255, 255, 255, 255}
Buffer.BlockCopy(header, 0, magicPacket, 0, header.Length)
' repeat the destination MAC 16 times
' your MAC *is* in network (reverse) order, right??
Dim offset As Integer = 6
Dim i As Integer = 0
Do While (i < 16)
Buffer.BlockCopy(targetMAC, 0, magicPacket, offset, targetMAC.Length)
offset = (offset + 6)
i = (i + 1)
Loop
If (Not (password) Is Nothing) Then
Buffer.BlockCopy(password, 0, magicPacket, offset, password.Length)
End If
Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Broadcast, 9)
Dim c As UdpClient = New UdpClient
c.Send(magicPacket, magicPacket.Length, ep)
End Sub
Does this look good?
However, I am bit confused as to how to format the MAC Adress when calling the sub..
WOL("00-1B-FC-CB-49-6B") wont work because its not supposed to be a string. Do I have to convert the MAC from Hex to Binary..or?
-
Nov 18th, 2009, 10:47 AM
#5
Re: WakeOnLAN - How to send packet?
I have VB.NET code to send a WOL magic packet around here somewhere. I can dig it up if nothing above works for you.
-
Nov 18th, 2009, 10:52 AM
#6
Thread Starter
Lively Member
Re: WakeOnLAN - How to send packet?
Originally Posted by kleinma
I have VB.NET code to send a WOL magic packet around here somewhere. I can dig it up if nothing above works for you.
That would be lovely! Again though, I am not sure if it will work in CF, but Its worth a shot
-
Nov 18th, 2009, 11:33 AM
#7
Re: WakeOnLAN - How to send packet?
Originally Posted by kleinma
I have VB.NET code to send a WOL magic packet around here somewhere. I can dig it up if nothing above works for you.
Take a look at my post above this one, that link I provided goes to another forum where you posted that very same magic packet code kleinma back in June 2007.
-
Nov 18th, 2009, 11:36 AM
#8
Re: WakeOnLAN - How to send packet?
Originally Posted by Jenner
Take a look at my post above this one, that link I provided goes to another forum where you posted that very same magic packet code kleinma back in June 2007.
Im sure it is the same code, so I guess no need to dig
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|