局域网唤醒无法正常工作 [英] Wake On LAN not working

查看:99
本文介绍了局域网唤醒无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我正在使用此代码..

但它不起作用..我已检查数据包是通过保持远程电脑开启而在远程计算机上接收...但是当远程电脑转动时关闭它没有开启

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

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

推荐答案

首先,WOL数据包是5个255字节,然后是16个重复的MAC地址BYTES(不是MAC的字符串表示)。



您放入数据包的字符串是一个mac地址字符串,而不是mac地址本身。您需要将mac地址转换为其字节,而不仅仅是将mac地址的字符串添加到数据包中。例如,你把字符串



AE2F在mac地址中添加字节65,69,50和70(ascii表示的字母),当你真的想要两个字节174和47.



你做错的最后一部分是将它发送到计算机的IP地址,计算机没有IP地址当它关闭以便没有任何内容可以发送时,你只能在网络广播地址(IPAddress.Broadcast)上广播数据包。
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).


这篇关于局域网唤醒无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆