远程唤醒
远程唤醒技术(WOL,Wake-on-LAN) 是由网卡配合其他软硬件,可以通过局域网实现远程开机的一种技术,
无论被访问的计算机离我们有多远、处于什么位置,只要处于同一局域网内,就都能够被随时启动。
这种技术非常适合具有远程网络管理要求的环境,如果有这种要求在选购网卡时应注意是否具有此功能。
可被远程唤醒的计算机对硬件有一定的要求,主要表现在网卡、主板和电源上。
1.网卡 能否实现远程唤醒,其中最主要的一个部件就是支持WOL的网卡。
远端被唤醒计算机的网卡必须支持WOL,而用于唤醒其他计算机的网卡则不必支持WOL。
另外,当一台计算机中安装有多块网卡时,只将其中的一块设置为可远程唤醒。
2.主板 主板也必需支持远程唤醒,可通过查看CMOS的"Power Management Setup"菜单中是否拥有
"Wake on LAN"项而确认。另外,支持远程唤醒的主板上通常都拥有一个专门的3芯插座,
以给网卡供电(PCI2.1标准)。 由于现在的主板通常支持PCI 2.2标准,
可以直接通过PCI插槽向网卡提供+3.3V Standby电源,即使不连接WOL电源线也一样能够实现远程唤醒,
因此,可能不再提供3芯插座。主板是否支持PCI2.2标准,
可通过查看CMOS的"Power Management Setup"菜单中是否拥有"Wake on PCI Card"项来确认。
3.电源 若欲实现远程唤醒,计算机安装的必须是符合ATX 2.01标准的ATX电源,
+5V Standby电流至少应在600mA以上。
计算机拥有远程唤醒功能并不等于能够实现远程唤醒,在实现之前,
还必须对硬件作必要的连接和设置。以及需要借助相应的软件才能实现。
例如使用AMD公司的Magic Packet来作为远程唤醒软件等
* created: 2 : 6 : 2004 10 : 24
* filename: c:documents and settingsadministrator桌面wakeup.cpp
* file path: c:documents and settingsadministrator桌面
* file base : wakeup
* file ext: cpp
* author: XiaoPing Zhang
*
* purpose:
*********************************************************************/
#include " stdafx.h "
#include
#define MAC_ADDR_LEN 6
#define MAGIC_DATA_LEN 102
#pragma comment(lib, "iphlpapi.lib")
BOOL GetMacFromIP( const char * pIP)
... {
HRESULT hr;
IPAddr ipAddr;
ULONG pulMac[2];
ULONG ulLen;
ipAddr = inet_addr ("216.145.25.31");
memset (pulMac, 0xff, sizeof (pulMac));
ulLen = 6;
hr = SendARP (ipAddr, 0, pulMac, &ulLen);
printf ("Return %08x, length %8d ", hr, ulLen);
size_t i, j;
char * szMac = new char[ulLen*3];
PBYTE pbHexMac = (PBYTE) pulMac;
//
// Convert the binary MAC address into human-readable
//
for (i = 0, j = 0; i < ulLen - 1; ++i) ...{
j += sprintf (szMac + j, "%02X:", pbHexMac[i]);
}
sprintf (szMac + j, "%02X", pbHexMac[i]);
printf ("MAC address %s ", szMac);
delete [] szMac;
return TRUE;
}
// 输入6个字节的mac地址
BOOL WakeupSinglePC( const unsigned char * pMac)
... {
// TODO: Add your command handler code here
if (pMac == NULL)
...{
TRACE("Mac address error!");
return FALSE;
}
WSADATA wsaData;
int err = WSAStartup( MAKEWORD(2, 2), &wsaData );
if ( err != 0 )
...{
TRACE("WSAStartup error %d !", WSAGetLastError());
return FALSE;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
...{
TRACE("WinSock DLL not supports 2.2 !");
return FALSE;
}
do
...{
SOCKET sFirst = socket(AF_INET, SOCK_DGRAM, 0);
if (sFirst == INVALID_SOCKET)
...{
TRACE("socket error %d !", WSAGetLastError());
break;
}
do
...{
BOOL bOptVal = TRUE;
int iOptLen = sizeof(BOOL);
err = setsockopt(sFirst, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, iOptLen);
if (err == SOCKET_ERROR)
...{
TRACE("setsockopt error %d !", WSAGetLastError());
break;
}
char szMagicData[MAGIC_DATA_LEN];
memset(szMagicData, 0xff, sizeof(szMagicData));
for(int i=MAC_ADDR_LEN; i ...{
memcpy(szMagicData+i, pMac, sizeof(unsigned char)*MAC_ADDR_LEN);
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(0);
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
err = sendto(sFirst, szMagicData, sizeof(szMagicData), 0, (LPSOCKADDR)&addr, sizeof(addr));
if (err == SOCKET_ERROR)
...{
TRACE("sendto error %d !", WSAGetLastError());
break;
}
} while(0);
err = closesocket(sFirst);
if (err == SOCKET_ERROR)
...{
TRACE("closesocket error %d !", WSAGetLastError());
break;
}
} while(0);
err = WSACleanup();
if ( err == SOCKET_ERROR )
...{
TRACE("WSACleanup error %d !", WSAGetLastError());
return FALSE;
}
return TRUE;
}
// 输入6个字节的mac地址数组
BOOL WakeupMultiPC( int iNum, const unsigned char * pszMac[])
... {
// TODO: Add your command handler code here
if (pszMac == NULL)
...{
TRACE("Mac address Error!", WSAGetLastError());
return FALSE;
}
WSADATA wsaData;
int err = WSAStartup( MAKEWORD(2, 2), &wsaData );
if ( err != 0 )
...{
TRACE("WSAStartup Error %d !", WSAGetLastError());
return FALSE;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
...{
TRACE("WinSock DLL not supports 2.2");
return FALSE;
}
do
...{
SOCKET sFirst = socket(AF_INET, SOCK_DGRAM, 0);
if (sFirst == INVALID_SOCKET)
...{
TRACE("socket error %d !", WSAGetLastError());
break;
}
do
...{
BOOL bOptVal = TRUE;
int iOptLen = sizeof(BOOL);
err = setsockopt(sFirst, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, iOptLen);
if (err == SOCKET_ERROR)
...{
TRACE("setsockopt error %d !", WSAGetLastError());
break;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(0);
addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
char szMagicData[MAGIC_DATA_LEN];
for (int index=0; index ...{
memset(szMagicData, 0xff, sizeof(szMagicData));
for(int i=MAC_ADDR_LEN; i ...{
memcpy(szMagicData+i, pszMac[index], sizeof(unsigned char)*MAC_ADDR_LEN);
}
err = sendto(sFirst, szMagicData, sizeof(szMagicData), 0, (LPSOCKADDR)&addr, sizeof(addr));
if (err == SOCKET_ERROR)
...{
TRACE("sendto error %d !", WSAGetLastError());
break;
}
}
} while(0);
err = closesocket(sFirst);
if (err == SOCKET_ERROR)
...{
TRACE("closesocket error %d !", WSAGetLastError());
break;
}
} while(0);
err = WSACleanup();
if ( err == SOCKET_ERROR )
...{
TRACE("WSACleanup error %d !", WSAGetLastError());
return FALSE;
}
return TRUE;
}
求教-vb实现远程唤醒 VB - 网络编程 - CSDN社区 community_csdn_net.mht
'类模块 CMagicPacket
'************************************************************************
'
' CMagicPacket - AMD Magic Packet (Wake-On-Lan) Class Module (v1.1)
' By Richard Larson, 1999/2000 - Requires VB 6.0 or better.
'
' This class encapsulates the functionality necessary to send Wake-on-Lan
' packets to WFM (Wired-For-Management) enabled computers that reside on
' an IP network.
'************************************************************************
'
' Version History:
'
' 1.1 - Fixed a bug that prevented IP addresses with individual octet
' values of zero from being properly validated in the
' ValidateIPAddress() function.
' 1.0 - Initial release to Internet.
'
'************************************************************************
'
' External Class Dependencies:
'
' This class depends on the existance of a winsock control on a form
' somewhere in the project. Each instance of this object must have a
' reference set to such a winsock control before it can be used to
' wake-up a workstation. This is accomplished via the WinsockControl
' property of the class.
'************************************************************************
'
' To create this class, open a new VB6 project and add a class module to
' it. Name the class module "CMagicPacket" and then paste this code
' snippet into it. Then save the class and add it to any project that
' needs this functionality.
'
' To use an object of this class, you must provide an MS Winsock control
' on one of your forms and then set the WinSockControl property to it as
' a reference. Then you simply set the NICAddress, IPSubnetAddress and
' IPSubnetMask properties of the new object and call the wakeup method.
'WinsockControl
' Example:
'
' Dim WOL_WS as CMagicPacket
' Set WOL_WS = New CMagicPacket
' With WOL_WS
' .WinsockCtrl=Winsock1
' .NICAddress="0008d7aa4e24"
' .IPSubnetAddress="172.17.1.0"
' .IPSubnetMask="255.255.255.0"
' .WakeUp
' End With
' Set WOL_WS=Nothing
'
' If the IPSubnetAddress or IPSubnetMask properties are left blank
' (or are determined to be invalid), the packet will be broadcast on
' the IP subnet local to the workstation that the object is running on.
'************************************************************************
'Object-level constants and enums...
Private Const ERROR_BASE = vbObjectError + 2000
Private Const ERROR_INVALID_NIC_ADDRESS = ERROR_BASE + 1
Private Const ERROR_INVALID_IP_SUBNET_ADDRESS = ERROR_BASE + 2
Private Const ERROR_INVALID_IP_SUBNET_MASK = ERROR_BASE + 3
Private Const ERROR_INVALID_WINSOCK_REFERENCE = ERROR_BASE + 4
Private Const ERROR_NO_WINSOCK_REFERENCE_AVAILABLE = ERROR_BASE + 5
'Object-level variables...
Private m_WinsockControl As Control
Private m_NICAddress As String
Private m_IPSubnetAddress As String
Private m_IPSubnetMask As String
'************************************************************************
' Property WinsockControl()
'
' This is the property assignment function (LET only) for the Winsock
' Control property. It validates that the winsock control being
' assigned is really a winsock control, and that it is not set to
' "nothing". If either of these checks fails, an appropriate error
' message is raised back to the caller.
'************************************************************************
Public Property Let WinsockControl(ByRef RefWinsock As Control)
'Check for uninitialized controls...
If RefWinsock Is Nothing Then
Err.Raise ERROR_NO_WINSOCK_REFERENCE_AVAILABLE, _
"CMagicPacket.WinsockControl(LET)", _
"An attempt was made to set the WinsockControl property " & _
"to an uninitialized control"
End If
'Check for invalid control types...
If TypeOf RefWinsock Is Winsock Then
Set m_WinsockControl = RefWinsock
Else
Err.Raise ERROR_INVALID_WINSOCK_REFERENCE, _
"CMagicPacket.WinsockControl(LET)", _
"An attempt was made to set the WinsockControl property " & _
"to a non-winsock control"
End If
'It passed the test, so set our reference to it...
Set m_WinsockControl = RefWinsock
End Property
'************************************************************************
' Property IPSubnetAddress()
'
' These are the assignment functions for the IP Address property. LET
' validates the IP subnet address being assigned and raises an error back
' to the calling function if it was not a valid 32-bit IP address. GET
' returns the current address in dotted-decimal notation (www.xxx.yyy.zzz)
'(www.xxx.yyy.zzz)
'************************************************************************
Public Property Let IPSubnetAddress(ByVal NewAddress As String)
'Set the new IP subnet address...
m_IPSubnetAddress = ValidateIPAddress(NewAddress)
'And raise an error if it is not valid...
If m_IPSubnetAddress = "" Then
Err.Raise ERROR_INVALID_IP_SUBNET_ADDRESS, _
"CMagicPacket.IPSubnetAddress(LET)", _
"Invalid IP Subnet Address"
End If
End Property
Public Property Get IPSubnetAddress() As String
IPSubnetAddress = m_IPSubnetAddress
End Property
'************************************************************************
' Property IPSubnetMask()
'
' These are the assignment functions for the IP Subnet Mask property. LET
' validates the IP subnet mask being assigned and raises an error back to
' the calling function if it was not a valid 32-bit IP mask address. GET
' returns the current address in dotted-decimal notation (www.xxx.yyy.zzz)
'************************************************************************
Public Property Let IPSubnetMask(ByVal NewMask As String)
'Set the new IP subnet address...
m_IPSubnetMask = ValidateIPAddress(NewMask)
'And raise an error if it is not valid...
If m_IPSubnetAddress = "" Then
Err.Raise ERROR_INVALID_IP_SUBNET_MASK, _
"CMagicPacket.IPSubnetMask(LET)", _
"Invalid IP Subnet Address"
End If
End Property
Public Property Get IPSubnetMask() As String
IPSubnetMask = m_IPSubnetMask
End Property
'************************************************************************
' Property NICAddress()
'
' These are the assignment functions for the NIC Address property. LET
' validates the NIC Address being assigned and raises an error back to
' the calling function if it was not a valid 48-bit NIC address. GET
' returns the 48-bit unformatted hex address in the form "1A2B3C4D5E6F"
'************************************************************************
Public Property Let NICAddress(ByVal NewNicAddress As String)
'Set the new NIC address...
m_NICAddress = ValidateNICAddress(NewNicAddress)
'And raise an error if it was not valid...
If m_NICAddress = "" Then
Err.Raise ERROR_INVALID_NIC_ADDRESS, _
"CMagicPacket.NicAddress(Let)", _
"Invalid Nic Address"
End If
End Property
Top
2 楼aohan(aohan)回复于 2005-05-22 00:46:27 得分 0 Public Property Get NICAddress() As String
NICAddress = m_NICAddress
End Property
'************************************************************************
' Method WakeUp()
'
' This method is called in order to send the wake-on-lan magic packet to
' the machine with the 48-bit hardware address specified in NICAdress
' residing on the IP subnet specified by the IPSubnetAddress/IPSubnetMask
' properties. It uses the winsock control that was set in the
' WinSockCtrl property to do the actual packet transfer. An error flag
' will be raised back to the caller if any of these properties are set
' incorrectly.
'************************************************************************
Public Sub WakeUp()
Dim ActualNICAddress As String
'Setup error handler...
On Error GoTo Error_Handler1
'Check for valid properties before sending the packet...
ActualNICAddress = ValidateNICAddress(m_NICAddress)
If ActualNICAddress = "" Then
Err.Raise ERROR_INVALID_NIC_ADDRESS, _
"CMagicPacket.Wakeup()", _
"The NICAddress property does not contain a " & _
"valid NIC address"
End If
If m_WinsockControl Is Nothing Then
Err.Raise ERROR_NO_WINSOCK_REFERENCE_AVAILABLE, _
"CMagicPacket.Wakeup()", _
"The WinsockCtrl property has not been set to " & _
"a valid MS-Winsock control"
End If
'Everything is OK, so send the packet on its way...
SendMagicPacketTo m_IPSubnetAddress, m_IPSubnetMask, _
m_NICAddress, m_WinsockControl
Exit Sub
Error_Handler1:
'For now, just return the error to the caller with a modified source...
' Err.Raise Err.Number, Err.Source & " <Raised From CMagicPacket.WakeUp()>", _
Err.Description , Err.HelpFile, Err.HelpContext
End Sub
'************************************************************************
' SendMagicPacketTo()
'
' Send a "Magic-packet" to a workstation that supports the "wake-on-lan"
' specification. This routine takes the IP address of the subnet on
' which the workstation to be "awakened" resides, and the 48-bit ethernet
' hardware address of the workstation's NIC. It then builds and sends a
' "magic packet" to that workstation which will wake it up (as though
' someone switched on the power).
'************************************************************************
Private Sub SendMagicPacketTo(IPSubnetAddress As String, _
IPSubnetMask As String, _
HWAddress As String, _
WinsockCtrl As Winsock)
'Local Variables...
Dim MagicPacketData(0 To 101) As Byte
Dim HWAddressByteValues(0 To 5) As Byte
Dim ByteIndex As Byte
Dim NICAddressIndex As Byte
'Setup error handler...
On Error GoTo Error_Handler
'First convert the Ethernet HWAddress to a byte array...
HWAddressStrToByteArray HWAddress, HWAddressByteValues()
'Now get the IP broadcast address for the IP subnet we want to use...
IPSubnetAddress = IPSubnetAddressToBroadcastAddress(IPSubnetAddress, IPSubnetMask)
'Setup the Magic-Packet synchronization byte sequence (0xFFFFFFFFFFFF)...
For ByteIndex = 0 To 5
MagicPacketData(ByteIndex) = 255
Next
'Now add the Magic-Packet HWAddress sequence (16 HW Addresses in a row)...
For NICAddressIndex = 0 To 15
For ByteIndex = 0 To 5
MagicPacketData((NICAddressIndex * 6) + ByteIndex + 6) = _
HWAddressByteValues(ByteIndex)
Next
Next
'Send a datagram containing the magic-packet data to the appropriate subnet...
m_WinsockControl.RemoteHost = IPSubnetAddress
m_WinsockControl.Protocol = sckUDPProtocol
m_WinsockControl.SendData MagicPacketData
m_WinsockControl.Close
Exit Sub
Error_Handler:
'For now, just return the error back to the caller with a modified source...
Err.Raise Err.Number, Err.Source & _
" <Raised From CMagicPacket.SendMagicPacketTo()>", _
Err.Description, Err.HelpFile, Err.HelpContext
End Sub
'************************************************************************
' ValidateNICAddress()
'
' This function takes a string and determines if it is a valid NIC
' address by checking it's length (6-hex bytes, 48-bits) and that it is
' a valid hexadecimal value (each nibble is 0-9, or A-F). If all is OK,
' the function returns a cleaned version of the address (with no
' whitespace characters and all upper case values), otherwise it returns
' a blank zero-length string.
'
' Based on this routine, all of the following addresses are valid:
'************************************************************************
Private Function ValidateNICAddress(AddressToValidate As String) As String
'Local Constants...
Const VALID_NIC_ADDRESS_LENGTH = 12
'Local Variables...
Dim HexCharIndex As Long
Dim HexChar As String
Dim InitialAddress As String
Dim ReducedAddress As String
'Init local variables...
ReducedAddress = ""
InitialAddress = UCase$(Trim$(AddressToValidate))
'Eliminate all non-hexadecimal characters from the address...
For HexCharIndex = 1 To Len(InitialAddress)
HexChar = Mid$(InitialAddress, HexCharIndex, 1)
Select Case HexChar
Case "0" To "9", "A" To "F": ReducedAddress = ReducedAddress & HexChar
End Select
Next
'Return the appropriate value...
If Len(ReducedAddress) = VALID_NIC_ADDRESS_LENGTH Then
ValidateNICAddress = UCase$(ReducedAddress)
Else
ValidateNICAddress = ""
End If
End Function
'************************************************************************
' ValidateIPAddress()
'
' This function takes an IP address in dotted decimal format and verifies
' that there are 4 octets of data and that the value of each octet is
' between zero and 255. This function does NOT determine whether the
' given address is legal for any particular subnetting scheme. If the
' address is valid, it will return the address itself, otherwise it will
' return an empty (zero-length) string.
'************************************************************************
Top
3 楼aohan(aohan)回复于 2005-05-22 00:46:58 得分 0 Private Function ValidateIPAddress(AddressToValidate As String) As String
'Local Variables...
Dim TempStr As String
Dim ReducedAddress As String
Dim SplitAddress() As String
Dim OctetIndex As Long
Dim OctetValue As Long
'First remove all non-decimal, non-period characters from the address...
For OctetIndex = 1 To Len(AddressToValidate)
TempStr = Mid$(AddressToValidate, OctetIndex, 1)
Select Case TempStr
Case "0" To "9", ".": ReducedAddress = ReducedAddress & TempStr
End Select
Next
'Split the octets into a string array for inspection...
SplitAddress = Split(ReducedAddress, ".")
ReducedAddress = ""
'If there are not exactly 4 octets (0->3), return empty...
If UBound(SplitAddress()) <> 3 Then
ValidateIPAddress = ""
Exit Function
End If
'Verify that the value of each octet is between 0 and 255...
For OctetIndex = 0 To 3
TempStr = SplitAddress(OctetIndex)
OctetValue = Val(TempStr)
If Len(TempStr) = 0 Or OctetValue < 0 Or OctetValue > 255 Then
ValidateIPAddress = ""
Exit Function
End If
ReducedAddress = ReducedAddress & Format$(OctetValue, "##0")
If OctetIndex < 3 Then ReducedAddress = ReducedAddress & "."
Next
'Return the validated result...
ValidateIPAddress = ReducedAddress
End Function
'************************************************************************
' HWAddressStrToByteArray()
'
' This routine takes a hexadecimal ethernet NIC address in string format
' and converts it into a 6-element (0 to 5) array of byte values
' representing that same address. This routine is used when building
' wake-on-lan "Magic-Packets".
'************************************************************************
Private Sub HWAddressStrToByteArray(HexHWAddress As String, _
ByRef HWAddressByteArray() As Byte)
'Local Variables...
Dim ByteIndex As Long
'Clear out the array to start...
For ByteIndex = 0 To 5
HWAddressByteArray(ByteIndex) = 0
Next
'Convert each hex double-digit to a byte in the array...
For ByteIndex = 1 To 11 Step 2
HWAddressByteArray((ByteIndex - 1) / 2) = _
Val("&H" & Mid$(HexHWAddress, ByteIndex, 2))
Next
End Sub
'************************************************************************
' IPSubnetAddressToBCastAddress()
'
' This routine takes an IP address and it's associated subnet mask and
' returns the directed-broadcast address for that subnet. If either
' parameter is invalid the local broadcast address (255.255.255.255)
' is returned.
'************************************************************************
Private Function IPSubnetAddressToBroadcastAddress(IPSubnetAddress As String, _
IPSubnetMask As String) _
As String
'Local Variables...
Dim IPStrArray() As String
Dim MaskStrArray() As String
Dim BCastAddressByte As Byte
Dim BCastAddress As String
Dim ByteIndex As Long
'Check for valid input...
If ValidateIPAddress(IPSubnetAddress) = "" Or _
ValidateIPAddress(IPSubnetMask) = "" Then
IPSubnetAddressToBroadcastAddress = "255.255.255.255"
Exit Function
End If
'Initialize the strings...
BCastAddress = ""
IPStrArray = Split(IPSubnetAddress, ".")
MaskStrArray = Split(IPSubnetMask, ".")
'Build the new broadcast address byte-by-byte...
For ByteIndex = 0 To 3
BCastAddressByte = Val(IPStrArray(ByteIndex)) Or _
(Val(MaskStrArray(ByteIndex)) Xor 255)
BCastAddress = BCastAddress & CStr(BCastAddressByte)
If ByteIndex < 3 Then BCastAddress = BCastAddress & "."
Next
'And finally return it to the calling routine...
IPSubnetAddressToBroadcastAddress = BCastAddress
End Function
'窗体测试
Private Sub Command1_Click()
Dim WOL_WS As CMagicPacket
Set WOL_WS = New CMagicPacket
With WOL_WS
.WinsockControl = Winsock1
.NICAddress = "0030f1983422"
.IPSubnetAddress = "192.168.0.1" '¶Ô·½IP
.IPSubnetMask = "255.255.255.0"
.WakeUp
End With
Set WOL_WS = Nothing
End Sub