chef 3810 Posted March 10, 2014 Posted March 10, 2014 (edited) Here is some code: Simple! First is the WOL Code: Imports System.Net Imports System.Net.Sockets Imports System.Text Module Module1 Sub Main() Dim ServerUp As New WakeUpServer ServerUp.macAddress = MAC_ADDRESS_HERE ServerUp.wakeIt() End Sub Private Class WakeUpServer Const lenHeader As Integer = 6 Const lenMAC As Integer = 6 Const repMAC As Integer = 16 Dim mEndPoint As IPEndPoint Dim mMACAddress As Byte() Dim mBytesSent As Integer = 0 Dim mPacketSent As String ''' <summary> ''' The IPEndPoint object that will act as a trasport for the packet. ''' It is automatically created by New statement, but you can modify it or read it. ''' </summary> ''' <value>An IPEndPoint object</value> ''' <returns>An IPEndPoint object</returns> ''' <remarks>Normally there is no need to change this manually.</remarks> Public Property endPoint() As IPEndPoint Get Return mEndPoint End Get Set(ByVal value As IPEndPoint) mEndPoint = value End Set End Property ''' <summary> ''' The target machine Network Interface Card MAC address. ''' It must be dash-separated, i.e. in the 11-22-33-44-55-66 form ''' </summary> ''' <value>A string with dash-separated values</value> ''' <returns>A string with dash-separated values</returns> ''' <remarks>The standard (IEEE 802) separator for cotet are dash (-) and semicolon (. I resolved to use dashes only in order to avoid any possible confusion and misunderstanding with upcoming IPv6 addressing space.</remarks> Public Property macAddress() As String Get Dim textMAC As New StringBuilder For Each currByte As Byte In mMACAddress textMAC.Append("-") textMAC.Append(currByte.ToString("X2")) Next Return textMAC.ToString.Substring(1) End Get Set(ByVal value As String) Dim values As Byte() For Each currByte As String In value.Split("-") If values Is Nothing Then ReDim values(0) Else ReDim Preserve values(values.GetUpperBound(0) + 1) End If values(values.GetUpperBound(0)) = Byte.Parse(currByte, Globalization.NumberStyles.HexNumber) Next mMACAddress = values End Set End Property ''' <summary> ''' Total bytes sent by WakeIt method. It is 0 until the method is called at least once for this class instance. ''' </summary> ''' <returns>Integer value, total bytes trasmitted</returns> ''' <remarks></remarks> Public ReadOnly Property bytesSent() As Integer Get Return mBytesSent End Get End Property ''' <summary> ''' It represent the Magic Packet broadcasted. ''' </summary> ''' <returns>String containing the text parsing of the Magic Packet</returns> ''' <remarks></remarks> Public ReadOnly Property packetSent() As String Get Return mPacketSent End Get End Property ''' <summary> ''' Creates a WOL Magic Packet, the datagram that will awake the target PC updon broadcast on the network. ''' </summary> ''' <param name="macAddress">An array of byte representing the target machine Network Interface Card MAC address</param> ''' <returns>An array of byte representing the Magic Packet</returns> ''' <remarks>This method can be used indipendently from the rest of the class. If necessary it can create a Magic Packet just providing the MAC address.</remarks> Public Function magicPacket(ByVal macAddress As Byte()) As Byte() Dim payloadData As Byte() Dim packet As New StringBuilder Try ReDim payloadData(lenHeader + lenMAC * repMAC) For i As Integer = 0 To lenHeader - 1 payloadData(i) = Byte.Parse("FF", Globalization.NumberStyles.HexNumber) Next For i As Integer = 0 To repMAC - 1 For j As Integer = 0 To lenMAC - 1 payloadData(lenHeader + i * lenMAC + j) = macAddress(j) Next Next For Each currLoad As Byte In payloadData packet.Append("-") packet.Append(currLoad.ToString("X2")) Next mPacketSent = packet.ToString.Substring(1) Catch ex As Exception mPacketSent = "EXCEPTION: " & ex.ToString End Try Return payloadData End Function Function sendUDP(ByVal payload As Byte(), ByVal endPoint As IPEndPoint) As Integer Dim byteSend As Integer Dim socketClient As Socket If (payload IsNot Nothing) AndAlso (endPoint IsNot Nothing) Then socketClient = New Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp) socketClient.Connect(endPoint) byteSend = socketClient.Send(payload, 0, payload.Length, SocketFlags.None) socketClient.Close() Else byteSend = 0 End If Return byteSend End Function ''' <summary> ''' It is the main method of the class. It must be called after the MAC address has been set. It does not return any code, you can see the result of the operation with the bytesSent and packetSent properties of this class. ''' </summary> ''' <remarks></remarks> Public Sub wakeIt() mBytesSent = sendUDP(magicPacket(mMACAddress), mEndPoint) End Sub ''' <summary> ''' No parameter is required. The new statement just created the IPEndPoint object. ''' </summary> ''' <remarks>The default IPEndPoint transmit on port 7. Other choices for WOL are port 0 or 9</remarks> Sub New() mEndPoint = New IPEndPoint(IPAddress.Broadcast, 7) End Sub ''' <summary> ''' The IPEndPoint object is created to the specified port ''' </summary> ''' <param name="epPort">A valid port number</param> ''' <remarks>If the port number is invalid, the IPEndPoint is created to port 7. Ports normally used for WOL are 0, 7 or 9.</remarks> Sub New(ByVal epPort As Integer) If epPort >= 0 AndAlso epPort < 65535 Then mEndPoint = New IPEndPoint(IPAddress.Broadcast, epPort) Else mEndPoint = New IPEndPoint(IPAddress.Broadcast, 7) End If End Sub End Class End Module We need to get the MAC Address for this to Send a Magic Packet to: Well here is how you get MAC Address from an IP Address: Private Class GetMBServerMACAddress Inherits ProgramInfo Private Shared Function ServerName() As String Dim HostName As String Dim IpCheck = System.Net.Dns.GetHostEntry(MEDIA_BROWSER_SERVER_IP_ADDRESS_HERE) HostName = IpCheck.HostName Return HostName End Function Public Shared Function GetMACAddress() As String Dim MACAddress As String Dim ComputerName = ServerName() Dim theManagementScope As New ManagementScope("\\" & ComputerName & "\root\cimv2") Const theQueryString As String = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 1" Dim theObjectQuery As New ObjectQuery(theQueryString) Dim theSearcher As New ManagementObjectSearcher(theManagementScope, theObjectQuery) Dim theResultsCollection As ManagementObjectCollection = theSearcher.Get() For Each currentResult As ManagementObject In theResultsCollection MessageBox.Show(currentResult("MacAddress").ToString()) MACAddress = (currentResult("MacAddress").ToString()) Next Return MACAddress End Function End Class But we still Need an Ip Address of the Media Browser Server: This can be attained from the API like this Dim ServerLocator = New ServerLocator Dim MediaBrowserServerProtocolAddress As String Try Dim ServerFind = Await ServerLocator.FindServer(CancellationToken.None) MediaBrowserServerProtocolAddress = ServerFind.Address.ToString Catch e As TaskCanceledException End Try There you go! You attained the IP of the Server from the API. This will have to IP will have to be gathered and recorded, because you can't get the IP if the Computer needs to be turned on, which is what we are doing. Edited March 10, 2014 by chef 1
chef 3810 Posted March 10, 2014 Author Posted March 10, 2014 Okay this is cool. I have the utility written like this: Imports System.Net Imports System.Net.Sockets Imports System.Text Module Module1 Sub Main() Dim ServerUp As New WakeUpServer ServerUp.macAddress = "00-30-1B-BD-9B-F2" ServerUp.wakeIt() End Sub Private Class WakeUpServer Const lenHeader As Integer = 6 Const lenMAC As Integer = 6 Const repMAC As Integer = 16 Dim mEndPoint As IPEndPoint Dim mMACAddress As Byte() Dim mBytesSent As Integer = 0 Dim mPacketSent As String ''' <summary> ''' The IPEndPoint object that will act as a trasport for the packet. ''' It is automatically created by New statement, but you can modify it or read it. ''' </summary> ''' <value>An IPEndPoint object</value> ''' <returns>An IPEndPoint object</returns> ''' <remarks>Normally there is no need to change this manually.</remarks> Public Property endPoint() As IPEndPoint Get Return mEndPoint End Get Set(ByVal value As IPEndPoint) mEndPoint = value End Set End Property ''' <summary> ''' The target machine Network Interface Card MAC address. ''' It must be dash-separated, i.e. in the 11-22-33-44-55-66 form ''' </summary> ''' <value>A string with dash-separated values</value> ''' <returns>A string with dash-separated values</returns> ''' <remarks>The standard (IEEE 802) separator for cotet are dash (-) and semicolon (. I resolved to use dashes only in order to avoid any possible confusion and misunderstanding with upcoming IPv6 addressing space.</remarks> Public Property macAddress() As String Get Dim textMAC As New StringBuilder For Each currByte As Byte In mMACAddress textMAC.Append("-") textMAC.Append(currByte.ToString("X2")) Next Return textMAC.ToString.Substring(1) End Get Set(ByVal value As String) Dim values As Byte() For Each currByte As String In value.Split("-") If values Is Nothing Then ReDim values(0) Else ReDim Preserve values(values.GetUpperBound(0) + 1) End If values(values.GetUpperBound(0)) = Byte.Parse(currByte, Globalization.NumberStyles.HexNumber) Next mMACAddress = values End Set End Property ''' <summary> ''' Total bytes sent by WakeIt method. It is 0 until the method is called at least once for this class instance. ''' </summary> ''' <returns>Integer value, total bytes trasmitted</returns> ''' <remarks></remarks> Public ReadOnly Property bytesSent() As Integer Get Return mBytesSent End Get End Property ''' <summary> ''' It represent the Magic Packet broadcasted. ''' </summary> ''' <returns>String containing the text parsing of the Magic Packet</returns> ''' <remarks></remarks> Public ReadOnly Property packetSent() As String Get Return mPacketSent End Get End Property ''' <summary> ''' Creates a WOL Magic Packet, the datagram that will awake the target PC updon broadcast on the network. ''' </summary> ''' <param name="macAddress">An array of byte representing the target machine Network Interface Card MAC address</param> ''' <returns>An array of byte representing the Magic Packet</returns> ''' <remarks>This method can be used indipendently from the rest of the class. If necessary it can create a Magic Packet just providing the MAC address.</remarks> Public Function magicPacket(ByVal macAddress As Byte()) As Byte() Dim payloadData As Byte() Dim packet As New StringBuilder Try ReDim payloadData(lenHeader + lenMAC * repMAC) For i As Integer = 0 To lenHeader - 1 payloadData(i) = Byte.Parse("FF", Globalization.NumberStyles.HexNumber) Next For i As Integer = 0 To repMAC - 1 For j As Integer = 0 To lenMAC - 1 payloadData(lenHeader + i * lenMAC + j) = macAddress(j) Next Next For Each currLoad As Byte In payloadData packet.Append("-") packet.Append(currLoad.ToString("X2")) Next mPacketSent = packet.ToString.Substring(1) Catch ex As Exception mPacketSent = "EXCEPTION: " & ex.ToString End Try Return payloadData End Function Function sendUDP(ByVal payload As Byte(), ByVal endPoint As IPEndPoint) As Integer Dim byteSend As Integer Dim socketClient As Socket If (payload IsNot Nothing) AndAlso (endPoint IsNot Nothing) Then socketClient = New Socket(endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp) socketClient.Connect(endPoint) byteSend = socketClient.Send(payload, 0, payload.Length, SocketFlags.None) socketClient.Close() Else byteSend = 0 End If Return byteSend End Function ''' <summary> ''' It is the main method of the class. It must be called after the MAC address has been set. It does not return any code, you can see the result of the operation with the bytesSent and packetSent properties of this class. ''' </summary> ''' <remarks></remarks> Public Sub wakeIt() mBytesSent = sendUDP(magicPacket(mMACAddress), mEndPoint) End Sub ''' <summary> ''' No parameter is required. The new statement just created the IPEndPoint object. ''' </summary> ''' <remarks>The default IPEndPoint transmit on port 7. Other choices for WOL are port 0 or 9</remarks> Sub New() mEndPoint = New IPEndPoint(IPAddress.Broadcast, 7) End Sub ''' <summary> ''' The IPEndPoint object is created to the specified port ''' </summary> ''' <param name="epPort">A valid port number</param> ''' <remarks>If the port number is invalid, the IPEndPoint is created to port 7. Ports normally used for WOL are 0, 7 or 9.</remarks> Sub New(ByVal epPort As Integer) If epPort >= 0 AndAlso epPort < 65535 Then mEndPoint = New IPEndPoint(IPAddress.Broadcast, epPort) Else mEndPoint = New IPEndPoint(IPAddress.Broadcast, 7) End If End Sub End Class End Module 'Imports System 'Imports System.Runtime.InteropServices 'Imports System.Threading 'Module Module1 ' Sub Main() ' SendMagicPacket() ' Console.ReadLine() ' End Sub 'Private Function GetIP(ByVal DNSName As String) As String ' Try ' Return Net.Dns.GetHostEntry(DNSName).AddressList.GetLowerBound(0).ToString ' Catch ex As Exception ' Return String.Empty ' End Try 'End Function 'Private Sub SendMagicPacket() ' 'SET THESE VARIABLES TO REAL VALUES ' Dim MacAddress As String = "00-30-1B-BD-9B-F2" ' Dim WANIPAddr As String = "192.168.0.4" '"24.235.162.129" ' Dim LanSubnet As String = "255.255.265.0" ' Dim Port As Integer = 9 ' 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 ' MacAddress = MacAddress.Replace("-", "").Replace(":", "") ' 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 ' Dim myAddress As String ' Try ' myAddress = Net.IPAddress.Parse(WANIPAddr).ToString ' Catch ex As Exception ' myAddress = GetIP(WANIPAddr) ' End Try ' If myAddress = String.Empty Then ' Console.WriteLine("Invalid IP address/Host Name given") ' Return ' End If ' Dim mySubnetArray() As String ' Dim sm1, sm2, sm3, sm4 As Int64 ' mySubnetArray = LanSubnet.Split("."c) ' For i = 0 To mySubnetArray.GetUpperBound(0) ' Select Case i ' Case Is = 0 ' sm1 = Convert.ToInt64(mySubnetArray(i)) ' Case Is = 1 ' sm2 = Convert.ToInt64(mySubnetArray(i)) ' Case Is = 2 ' sm3 = Convert.ToInt64(mySubnetArray(i)) ' Case Is = 3 ' sm4 = Convert.ToInt64(mySubnetArray(i)) ' End Select ' Next ' myAddress = WANIPAddr ' udpClient.Send(sendBytes, sendBytes.Length, myAddress, Port) 'End Sub On second run of this Application I'll have recorded the Serve rIp to an XML file. Then it will have set itself up automatically to Start the Server machine from an Icon on your desktop.
chef 3810 Posted March 10, 2014 Author Posted March 10, 2014 And here is how you get the MAC Address of your Media Browser Server Machine using the IP Address Private Class GetMBServerMACAddress Inherits ProgramInfo Private Shared Function ServerName() As String Dim HostName As String Dim IpCheck = System.Net.Dns.GetHostEntry(IP_ADDRESS_HERE) HostName = IpCheck.HostName Return HostName End Function Public Shared Function GetMACAddress() As String Dim MACAddress As String Dim ComputerName = ServerName() Dim theManagementScope As New ManagementScope("\\" & ComputerName & "\root\cimv2") Const theQueryString As String = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 1" Dim theObjectQuery As New ObjectQuery(theQueryString) Dim theSearcher As New ManagementObjectSearcher(theManagementScope, theObjectQuery) Dim theResultsCollection As ManagementObjectCollection = theSearcher.Get() For Each currentResult As ManagementObject In theResultsCollection MessageBox.Show(currentResult("MacAddress").ToString()) MACAddress = (currentResult("MacAddress").ToString()) Next Return MACAddress End Function End Class And Of coarse we all know how to get the IP address of the Server Machine from the API. So BAM! there is how to set up WOL in VB.Net.
chef 3810 Posted March 10, 2014 Author Posted March 10, 2014 So this is cool, Media Browser Kinect will attempt to start the Server Machine in order to Build it's Client interface! Just Awesome! 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now