Socket implementation Remote Wake (Wake-on-lan) Step by step

Source: Internet
Author: User
Tags socket error sprintf htons

Step 1: Understanding Remote wake-up

Computer Digest 2003.9-Application and Tips- remote wake-up a little pass

"Computer Application Digest" in 2003, the 7th issue of "strategist, winning thousands-remote control" a
This paper introduces how to realize remote control technology, but the first condition of remote control is that the remote computer must be in the state of hand. But it is unrealistic to have the remote computer boot up and wait for the client's connection 24 hours. Can you remotely control the computer when needed? The answer is yes. We can use remote wake-up technology to achieve this function.

There are two main ways to realize remote wake-up: wake-up on LAN (LAN boot, WOL) and wake-up on modem (modem boot, abbreviation WOM). The following author to introduce the remote wake-up technology implementation methods.

First, WOL

To enable the LAN to boot, the motherboard and the network card must all support remote wake-up capabilities. Generally, the current motherboard support this feature, supported motherboards usually have a dedicated 3-core socket, in order to power the network card when the shutdown. But
Not all network adapters support this feature (especially some inexpensive, low-end network adapters), the way to determine whether the NIC supports remote wake-up is simple, with a 3-pin WOL interface and a 3-core remote wake-up cable on the network card that supports remote wake-up, by determining if the NIC has a WOL interface ( Some newer network adapters may not have a WOL interface to support remote wake-up. This is because the current popular motherboard supports PCI2. 2 Standard, while PCI 2. The 2 standard does not require a dedicated WOL interface to power the NIC, allowing the motherboard to provide standby power to the NIC directly through the PCI slot.

1. Hardware Connection
After the NIC is installed, plug one end of the remote wake cable into the WOL interface of the NIC, and the other end is connected with the motherboard's 3-pin WOL Remote Wake interface (the interface is usually marked with Wol_con, of course, if the motherboard and NIC support PCI2. The 2 standard is not required to do this).

2. CMOS Settings
Turning on the CMOS remote wake feature is simple, just set the "Wake up on LAN" entry in the "Power Management Setup" in the CMOS settings to "Enable".

3. Remote wake-up Computer
The network card of the remote computer only receives special signal to activate the system, so we have to use the corresponding software to generate these data frames. A lot of similar software, the most famous is developed by AMD Magic Packet, in addition to some of the network card with the diagnostic program also has this function. The author here to recommend a Chinese people
Software developed-"Network Wake-up".
Network wake-up is a no need to install green software, first unzip the downloaded compressed package, and then run the "network Wake V041." EXE "Open the wake of the network." The software interface as shown above, because only know the remote computer's MAC address can be awakened, so first to make the software know the remote computer MAC address. Click Scan Network on the LAN menu, "Wake up" scans all computers on the local area network and displays information such as computer name, IP address, and MAC address in list information (if it is not scanned to the target computer, click "Add manually" on the LAN menu), When you are done, click Save List on the list menu to save the results of the scan for the next use.
To wake up a computer on your network, you can right-click the computer in list information and choose Wake. If you want to wake up all the computers in the list, you can choose Wake all.

Second, WOM

WOL can only be used in the local area network, if the remote wake-up computer distance from the local, we must through the WOM to achieve remote wake-up (it can be no exaggeration to say that all telephone access to the place of the ancestral use of WOM to achieve remote wake-up).
WOM also requires dual support for both the motherboard and modem (this feature is currently supported by most motherboards and modems).
The implementation of WOM is relatively simple, the modem connected to the computer and telephone line, the "Power Management Setup" in the CMOS in the "Power in the Ring" item set to "Enable". Now as long as the modem is in the open State, dial modem connected to the phone number can be realized by WOM remote boot. and using WOM for remote wake-up There's no need to worry about the phone bill, because the modem can start the machine if it detects the phone ringing without answering the phone (so we can make an international long-distance call to wake up the computer in another country without spending a penny).

Step 2: Principle of remote wake technology

Magic Packet™technology

The Magic packet™technology is used to remotely wake up a sleeping or powered off PC on a network. This is accomplished by sending a specific packet of information, called a Magic packet frame and to a node on the network. When a PC capable of receiving the specific frame goes to sleep, it would enable the Magic Packet mode in the LAN Controlle R, and when the LAN controller receives a Magic Packet frame, it would alert the system to wake up.

The patented Magic Packet technology is implemented entirely in the LAN controller. This is architecture allows the PC to go in a very low power mode, even as far as to remove the "power" from the entire syste M, except for the LAN chip.

Magic Packet Technology Details
Once the LAN controller has been put in the Magic Packet mode, it scans all incoming frames to the node for a Specific data sequence, which indicates to the controller of this is a Magic frame. A Magic Packet Frame must also meet the basic requirements for the LAN technology chosen, such as SOURCE address, Destinat ION address (which is the receiving station's IEEE address or a multicast address which the includes address ), and CRC. The specific sequence consists duplications of the IEEE address is this node, with no breaks or interruptions. This sequence can is located anywhere within the packet, but must is preceded by a synchronization stream. The synchronization stream allows the scanning state machine to is much simpler. The synchronization stream is defined as 6 bytes of FFh. The device'll also accept a broadcast frame, as long as the duplications of the IEEE address match the Machine to be awakened. If the IEEE addressFor a particular node on the network is 11h 22h 33h 44h 55h 66h then the LAN controller would is scanning for the data s Equence (assuming an Ethernet Frame):

Destination SOURCE MISC. FF FF FF FF FF 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 3 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 66 11 22 33 44 55 A-MISC-a-a-a-list of all of the numerous and a-I-TB. CRC.

There are no other restrictions on a Magic Packet frame. For instance, the sequence could is in a TCP/IP packet, an IPX packet, etc. The frame may is bridged or routed across the network, without affecting its ability to wake up a node at the destination The frame.

If the LAN controller scans a frame and does not find the specific sequence shown above, it discards the frame and takes n O further action. IF The controller detects the data sequence, however, then it alerts the PC ' s power management circuitry to wake up the SY Stem.

Magic Packet today
Hewlett-Packard, IBM, Gateway and other leading manufacturers implement AMD ' s Magic Packet. Magic Packet allows are managers to increase their efficiency from remotely any PCs on the accessing--on, network do WN, or powered off. Magic Packet Technology is currently offered on AMD ' s Pcnet™-isa II, Pcnet-pci II, Pcnet-fast, pcnet-fast+ and Pcnet-fast III, and is a standard feature that would be integrated into future AMD pcnet controllers.

With Magic Packet Technology, AMD set the standard and built the foundation for today's Advanced Power management Technolo Gies for networked PCs, including the OnNow power management industry. The OnNow Design Initiative was a comprehensive, system-wide approach to system and device. In fact, AMD co-authored the OnNow Power Management reference specification for networking devices to furthering our leaders Hip position in this industry initiative.

AMD licenses the Magic Packet technology to device manufacturers. If you are are interested in getting licensing information, please contact Rahul Deshmukh at (408) 749-5448 or e-mail at Rahul. Deshmukh@amd.com.

STEP3: Remote wake-up Technology Implementation

/********************************************************************
* CREATED:2004/06/02
* UPDATED:2006/03/28
* File Name:wakeup.cpp
* Author:xiaoping Zhang
* Purpose:
*********************************************************************/
#include "stdafx.h"
#include "iphlpapi.h"
#include <winsock2.h>

#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/n", 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/n", SZMAC);

delete [] Szmac;

return TRUE;
}

Enter a 6-byte MAC address
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;
}

Todo
{
Socket Sfirst = socket (af_inet, SOCK_DGRAM, 0);
if (Sfirst = = Invalid_socket)
{
TRACE ("Socket error%d!", WSAGetLastError ());
Break
}

Todo
{
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<magic_data_len; i+=mac_addr_len) {
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;
}

Enter a 6-byte MAC address array
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;
}

Todo
{
Socket Sfirst = socket (af_inet, SOCK_DGRAM, 0);
if (Sfirst = = Invalid_socket)
{
TRACE ("Socket error%d!", WSAGetLastError ());
Break
}

Todo
{
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<inum; index++) {
memset (Szmagicdata, 0xFF, sizeof (Szmagicdata));

for (int i=mac_addr_len; i<magic_data_len; i+=mac_addr_len) {
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
}
Sleep (0);
}

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;
}

Step4: Remote wake-up share software

http://download.enet.com.cn/html/010062001052902.html

STEP5: Development of Ideas

Http://www.scyld.com/wakeonlan.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.