Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am using this code..
but its not working.. i have checked Packets are receiving on remote computer by keeping remote pc turn on... but when the remote pc is turned off its not getting turn on
VB
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SendMagicPacket("IP", "MAC Addr", "Sub Net") 'Remote PC details
    End Sub

    Public Sub SendMagicPacket(ByVal ipAddress As String, ByVal strMacAddress As String, ByVal strLanSubnet As String)
        'SET THESE VARIABLES TO REAL VALUES
        Dim MacAddress As String = String.Empty
        Dim WANIPAddr As String = String.Empty
        Dim LanSubnet As String = String.Empty
        Dim strBroadcast As String = String.Empty

        'Ports to send magic packet to.
        Dim Port As Integer = 9
        Dim AltPort As Integer = 7

        Dim udpClient As New System.Net.Sockets.UdpClient
        Dim buf(101) As Char
        Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(buf)

        MacAddress = strMacAddress
        WANIPAddr = ipAddress
        LanSubnet = strLanSubnet

        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
            MsgBox("Packet send failed. see log")
            'log("Magic packet failed! ip address appears to be invalid! error: " & ex.Message)
            Exit Sub
        End Try

        If myAddress = String.Empty Then
            MessageBox.Show("Invalid IP address/Host Name given")
            Return
        End If

        Dim mySubnetArray() As String

        mySubnetArray = LanSubnet.Split(".")

        strBroadcast = CalculateBroadCastAddress(ipAddress, strLanSubnet)

        myAddress = WANIPAddr

        Try
            'log("Magic Packet Sent with data - ip:" & ipAddress & ", broadcast: " & strBroadcast & ", mac: " & strMacAddress & ", subnet: " & strLanSubnet)

            'Send magic packet to broadcast ip
            udpClient.Send(sendBytes, sendBytes.Length, strBroadcast, Port)
            udpClient.Send(sendBytes, sendBytes.Length, strBroadcast, AltPort)

            'Send magic packet to PC's ip
            udpClient.Send(sendBytes, sendBytes.Length, ipAddress, Port)
            udpClient.Send(sendBytes, sendBytes.Length, ipAddress, AltPort)

            udpClient = Nothing
            MsgBox("Magic Packet Sent! Computer should power on IF wake-on-lan is enabled.")

            Shell("ping -t " & ipAddress, AppWinStyle.NormalFocus)
        Catch ex As Exception
            MsgBox("Error sending macgic packet. error: " & ex.Message)
        End Try

    End Sub

    Public Function CalculateBroadCastAddress(ByVal currentIP As String, ByVal ipNetMask As String) As String
        Dim strCurrentIP As String() = currentIP.ToString().Split(".")
        Dim strIPNetMask As String() = ipNetMask.ToString().Split(".")
        Dim arBroadCast As ArrayList = New ArrayList()

        Dim iTotalSubnets As Integer = 2 ^ BorrowedBits(strIPNetMask(3))
        Dim iHosts As Integer = 2 ^ (8 - BorrowedBits(strIPNetMask(3)))
        Dim iSubNetNum As Integer
        Dim iIPOctet As Integer = strCurrentIP(3)

        If iIPOctet / iHosts < 1 Then
            iSubNetNum = 0
        Else
            iSubNetNum = (iIPOctet / iHosts) - ((iIPOctet / iHosts) Mod 1)
        End If

        For i As Integer = 0 To 3
            If i <> 3 Then
                arBroadCast.Add(strCurrentIP(i))
            Else
                arBroadCast.Add((iHosts * iSubNetNum) + (iHosts - 1))
            End If
        Next

        Return arBroadCast(0) & "." & arBroadCast(1) & "." & arBroadCast(2) & "." & arBroadCast(3)

    End Function

    Function BorrowedBits(ByVal iNum As Int32) As Int32
        Select Case iNum
            Case 255
                Return 8
            Case 254
                Return 7
            Case 252
                Return 6
            Case 248
                Return 5
            Case 240
                Return 4
            Case 224
                Return 3
            Case 192
                Return 2
            Case 128
                Return 1
            Case 0
                Return 0
        End Select
    End Function


End Class
Posted
Updated 19-May-13 20:24pm
v3
Comments
SoMad 15-May-13 3:31am    
Let me ask you a couple of basic questions about the PC you are trying to wake:
Does the hardware support WoL?
Is WoL Enabled in the BIOS?

Soren Madsen
Basmeh Awad 15-May-13 3:39am    
yes it supports WOL..
i have Enabled WOL in Power Management in BIOS and also in Network adapter setting
"Wake on Magic Packet"..is any more setting is to be done???
Basmeh Awad 18-May-13 8:50am    
????

1 solution

First, the WOL packet is 6 bytes of 255 followed by 16 repetitions of the MAC address BYTES (not the string representation of the MAC).

The string you are putting in the packet is a string of the mac address, not the mac address itself. You need to convert the mac address to its bytes, not just add the string of the mac address to the packet. For example, you are putting the string

AE2F in the mac address adds the bytes 65, 69, 50, and 70 (ascii representation of the letters), when you really want two bytes 174 and 47.

The last part you are doing wrong is sending it to the IP address of the computer, the computer does not have an IP address when it is turned off so there is nothing to send to, you only broadcast the packet out on the network broadcast address (IPAddress.Broadcast).
 
Share this answer
 
Comments
Basmeh Awad 18-May-13 9:41am    
i am taking MAC address as string but later in for loop chinging it into its ASCII values in a byte array "sendBytes[]" followed by 16 reputations..and
yep you are right..and i knew computer does not have an IP address when it is turned off bt also am taking ip address to ping it at last after sending the packet to check wether the system gets turned on or not..
Ron Beyer 20-May-13 14:14pm    
Changing an ASCII MAC Address to a byte array through ASCII.GetBytes is not the same as putting the bytes of the mac address in the packet. A MAC address 48 bits (6 bytes), the MAC you are adding to the packet is much longer because its probably 12 bytes (2 characters per byte represented in Hex). For example, lets say your MAC is 0013d362b9f3, you need to put in your packet the byte array {0x00, 0x13, 0xd3, 0x62, 0xb9, 0xf3}. Using ASCII you are actually putting in the byte array {0x00, 0x00, 0x31, 0x33, 0x64, 0x33, 0x36, 0x32, 0x62, 0x39, 0x66, 0x33}. When you form a proper magic packet, its data payload should be exactly 102 bytes, anything else and you've done something wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900