Results 1 to 8 of 8

Thread: WakeOnLAN - How to send packet?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    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

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    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?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    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?

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Re: WakeOnLAN - How to send packet?

    Quote Originally Posted by kleinma View Post
    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

  7. #7
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: WakeOnLAN - How to send packet?

    Quote Originally Posted by kleinma View Post
    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: WakeOnLAN - How to send packet?

    Quote Originally Posted by Jenner View Post
    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
  •  



Click Here to Expand Forum to Full Width