可以用VB.net发送普通的UDP数据包 - 但是你如何发送WOL数据包?

时间:2015-09-24 22:05:38

标签: vb.net network-programming udp wake-on-lan

我可以发送一个数据包:

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

效果很好并且使用WireShark我可以在端口9上看到UDP数据包被发送到我的IP(我在地址/字框中输入)。

我理解WOL(我已经工作了,只想创建我自己的应用程序以获得乐趣/体验)你需要一个MAC,它只是“添加MAC”地址,这使它成为一个WOL数据包或是它比那更复杂?

注意:我可以在Wireshark中看到WOL和UDP数据包的区别,并将我的IP更改为“MYIP”。例如,下面是从WOL应用程序发送的WOL数据包。

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

与普通UDP数据包相比:

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

编辑: 我用一些例子玩了很多,但它们并不是最好的。

1 个答案:

答案 0 :(得分:2)

几个小时后我设法做到了。如果其他人被卡住了,我就是这样做的:

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

就是这样。然后你所做的就是改变你的IP和MAC以满足你的需求。或者,如果需要,可以为不同的输入添加文本框。