Flickr logo
More of Luca Mauri's photos
blank

Section's icon A snippet
WOL .net
Codename: wol

Wake-On-LAN (WOL) is a feature of the network interface cards since the late '90s
This functionality was initially conceived by IBM and the widely supported by Intel with NIC's and motherboard's chipset

WOL simply works by broadcasting a so called Magic Packet on the LAN where the device you want to wake-up is on. The Magic Packet is made by a leading HEX string followed by 16 repetition of the target MAC Address.
For the following class to works you must simply create an instance, provide the MAC Address and call the method WakeIt.
The class also includes Intellisense description.

Separatore

Following the full code of the snippet
You can copy and paste it from here, otherwise you can download it in .snippet file format by clicking here.

                Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class clsWOL
    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                
            

Go back to snippet page.

Gear B/N Risultato del processo OK
blank

Ajax-enabled news - Beta

Ajax News temporaneamente sospese in attesa della nuova versione.

Tag Cloud - Beta

AddThis Bookmark

Il pulsante di AddThis permette di aggiungere un segnalibro per questa pagina nei più diffusi siti internet.

Licenza

Creative Commons License
Il contenuto di questo sito internet, se non diversamente specificato, è pubblicato sotto una Licenza Creative Commons.

The content of this website is published under a Creative Commons License, unless otherwise stated.

Site Meter

ClustrMaps

Locations of visitors to this page

Elenco Feed

Questo file OPML contiene l'elenco aggiornato di tutti i feed RSS disponibili sul sito.

OPML rect

amung.us

hit counter

Random Quote

«- Doesn't anybody ever go home?
- You're here.
- Exactly. Trust me, you don't want to model your social life on mine. »

Gideon, Elle
Criminal Minds - "The Aftermath"

Leggi tutte le citazioni

Random Fact

Il numero totale delle particelle nell'universo è circa 1080.
Sulla Terra esistono circa 1030 cellule viventi

von Bertalanffy (1969)

Leggi tutte le curiosità

aNobii Books

Sorting Hat

i'm in ravenclaw!
be sorted @ nimbo.net

LM CubeCopyright © 2000 - 2010 Luca Mauri. All rights reserved
Note sui contenuti del sito.


Generated by Luca Mauri's xCMS xCMS | ASP.net 2.0 powered Powered by ASP.net 2.0

Technology Blogs - BlogCatalog Blog Directory


Standard compliant: Valid Sitemaps | W3C CSS Valid | W3C XHTML Valid | RSS 2.0 Valid | OPML checked by validator.opml.org. | AJAX Powered


This page is generated by Luca Mauri Luca Mauri's xCMS
eXperimental Content Management System version 1.5.0.11480
Current date and time on server Fri, 03 Sep 2010 17:48:09 GMT
Took 1,0170296 seconds to be created