Search code examples
vb.netnetwork-programmingudpwake-on-lan

Can send normal UDP packet with VB.net - But how do you send a WOL packet?


I can send a packet with this:

Public Class Form1
Dim publisher As New Sockets.UdpClient(0)
Dim subscriber As New Sockets.UdpClient(2000)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    publisher.Connect(TBTo.Text, TBPort.Text)
    Dim sendbytes() As Byte = ASCII.GetBytes("test")
    publisher.Send(sendbytes, sendbytes.Length)
End Sub
End Class

It works great and using WireShark I can see on Port 9 a UDP Packet being sent to my IP (which I type into the address/word box).

I understand for WOL (Which I DO have working, just want to make my own application for fun/experience) you need a MAC, will it just "adding a MAC" address to this make it a WOL packet or is it more complicated than that?

NOTE: I can see the difference in WOL and UDP packets in Wireshark and changing my IP to "MYIP". For example below is a WOL packet sent from a WOL application.

40.000203000 MYIP 192.168.1.1 WOL 144 MagicPacket for Micro-St_21:94:b6 (44:8a:5b:21:94:b6)

Compared to a normal UDP packet:

69 12170.303276000 192.168.1.1 MYIP UDP 46 Source port: 57465 Destination port: 9

EDIT: I have played about A LOT with some examples but they are not the best.


Solution

  • I managed to do it after a few hours. If anyone else is stuck, here is how I did it:

    Public Class Form1
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MacAddress As String = "YOUR MAC ADDRESS HERE"
        Dim myAddress As String = "YOURIP HERE"
        Dim udpClient As New System.Net.Sockets.UdpClient
    
        Dim buf(101) As Char
    
        Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(buf)
    
        For x As Integer = 0 To 5
    
            sendBytes(x) = CByte("&HFF")
    
        Next
    
        Dim i As Integer = 6
    
        For x As Integer = 1 To 16
    
            sendBytes(i) = CByte("&H" + MacAddress.Substring(0, 2))
    
            sendBytes(i + 1) = CByte("&H" + MacAddress.Substring(2, 2))
    
            sendBytes(i + 2) = CByte("&H" + MacAddress.Substring(4, 2))
    
            sendBytes(i + 3) = CByte("&H" + MacAddress.Substring(6, 2))
    
            sendBytes(i + 4) = CByte("&H" + MacAddress.Substring(8, 2))
    
            sendBytes(i + 5) = CByte("&H" + MacAddress.Substring(10, 2))
    
            i += 6
    
        Next
    
        udpClient.Send(sendBytes, sendBytes.Length, myAddress, 9)
    End Sub
    End Class
    

    That is it. Then all you do is change your IP and MAC to suit your needs. Or if you need, you can add in text boxes for different inputs.