<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>remote wakeup (wake on LAN)</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b></span>
</font><span lang="en-ca"><font size="6" color="#FF0000"><b>Remote Wakeup (or 
Wake On LAN (WOL))</b></font></span></p>
<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
£¨This is a fake, false, fault or whatever, </b></font></span></p>
<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
it simply doesn't work!)</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b><font size="3">This is <span lang="en-ca">probably the most simple program I have posted in my personal website because it has a simple naive</span></font></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3"><b>function, namely to wake up a remote computer, by using the so-called wake on LAN or WOL. </b></font></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><b>Suppose that your supervisor gives you 
	some computers which are all running in different floors of building and you 
	happen to be doing a parallel computation job with those clusters, then will 
	you be happy to take elevator to jump from floor to floor to see what the 
	screen displays in each computer?</b></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><b>I think most of us won't like to do 
	that. Windows XP supplies with a nice, easy &quot;remote-desktop&quot; which is like 
	the ancient &quot;PC Anywhere&quot;. Then one problem arises here. Can you remotely 
	start the power and turn it off?</b></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><b>Turning off is not a big deal. One 
	simple solution is to run dos command &quot;shutdown&quot; or write a small program to 
	do it( Assuming you are as funny-about as I am with absolutely nothing to 
	do.)</b></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><b>How about turning on? It is not so 
	straight-forward and I am pretty sure it is not part of windows management 
	because I cannot find any clue in windows help or MSDN etc.</b></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><b>It is up to you to implement with a very 
	small step plus several hardware support.</b></span></div>
<div align="left">
  <p ALIGN="LEFT">¡¡</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  ¡¡</div>
<p><span lang="en-ca"><b>Here let me write down the configuration.</b></span></p>
<p><span lang="en-ca"><b>1. To turn on &quot;remote-desktop&quot;, you need to go to 
&quot;control-panel\system\remote&quot; and check &quot;allow users to connect remotely to this 
computer&quot;. </b></span></p>
<p><span lang="en-ca"><b>2. Make sure your user account is already granted with 
access by &quot;select remote users&quot; at same page. One simple trick in windows is 
that create same user with same password in all clusters. </b></span></p>
<p><span lang="en-ca"><b>3. Then you can use &quot;remote-desktop&quot; from 
&quot;accessory\communication\remote-desktop&quot;.</b></span></p>
<p><span lang="en-ca"><b>Here is the hardware support for &quot;remote-wakeup&quot; or 
&quot;wake on LAN&quot; and some of them are simply what I read from various sources in 
internet.</b></span></p>
<p><span lang="en-ca"><b>1. Your OS must support this. window XP is OK.</b></span></p>
<p><span lang="en-ca"><b>2. Your network adaptor must support it. To enable it, 
go to &quot;\local-network-connection\status\property&quot; and click &quot;configure&quot; and 
choose &quot;power management&quot; to check &quot;allow this device to bring this computer to 
be out of standby&quot;. Go to page &quot;advanced&quot; and choose &quot;wakeup compatibilities&quot; to 
make sure &quot;magic frame&quot; or &quot;magic packet&quot; is included.</b></span></p>
<p><span lang="en-ca"><b>3. Your motherboard or BIOS must support it. Reboot 
your computer and enter setup of BIOS to enable &quot;remote wakeup&quot; or &quot;boot on LAN&quot; 
or something similar (not quite sure).</b></span></p>
<p><span lang="en-ca"><b>4. Then the rest of it is just a little programming. 
The simple idea is to use UDP to send a special packet or &quot;magic packet&quot; to 
sleeping computer which only allow network adaptor monitoring incoming message. 
When NIC receives, it wakeup the OS. So, the understatement is that your 
hardware must support &quot;ATX&quot; or advanced power management.</b></span></p>
<p><span lang="en-ca"><b>5. What is the port number to send? It is by default 
40000. </b></span></p>
<p><span lang="en-ca"><b>6. What is composed of the packet? It consists of a 
header and 16 times repeating of the mac address of sleeping machine. The header 
is 6 bytes of 0xFF. And each mac address is also 6 bytes. Therefore total 6+16x6 
bytes.</b></span></p>
<p><span lang="en-ca"><b>7. How can I get mac address? And IP address of the 
machine? </b></span></p>
<p><span lang="en-ca"><b>I haven't found the way to convert IP to mac. And to 
acquire local mac address is simple. You can run &quot;getmac&quot; at command line. So, I 
simply hardcode everything.</b></span></p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>¡¡</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left" style="width: 898; height: 86">
  <pre><font size="3"><b><span lang="en-ca">1. wakeup</span>.cpp</b></font></pre>
  <pre>¡¡</pre>
  <pre><font size="3" color="#FF0000"><b><span lang="en-ca">file name: wake</span>.cpp</b></font></pre>
  <pre>#include &lt;winsock.h&gt;
#include &lt;iostream&gt;

using namespace std;

typedef unsigned char uchar;

const int MagicPacketLength=6+6*16;

const uchar MagicPacketHeader[6]=
{
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};

/* the address of nicktcl
const uchar macAddress[6]=
{
	0x00, 0x0F, 0xEA, 0xD0, 0xDF, 0x46
};
*/

const uchar macAddress[6]=
{
	0x00, 0x13, 0xD3, 0x99, 0x19, 0xBA
};

char packet[MagicPacketLength];

WSADATA wsadata;

const int PortNumber=40000;
int errCode;
SOCKET mySocket;
char buffer[32];
hostent* hostInfo;
sockaddr_in address;

void init()
{
	if ((errCode=WSAStartup(0x2020, &amp;wsadata))!=0)
	{
		switch (errCode)
		{
		case WSASYSNOTREADY:	
			printf(&quot;WSASYSNOTREADY\n&quot;);
			break;
		case WSAVERNOTSUPPORTED:
			printf(&quot;WSAVERNOTSUPPORTED\n&quot;);
			break;
		case WSAEINPROGRESS:
			printf(&quot;WSAEINPROGRESS\n&quot;);
			break;
		case WSAEPROCLIM:
			printf(&quot;WSAEPROCLIM\n&quot;);
			break;
		case WSAEFAULT:
			printf(&quot;WSAEFAULT\n&quot;);
			break;
		}
		exit(1);
	}

	if ((mySocket = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET) 		  
	{
		errCode=WSAGetLastError();
		exit(2);
	}


}

void assemblyPacket()
{
	memcpy(packet, MagicPacketHeader, 6);
	for (int i=1; i&lt;=16; i++)
	{
		memcpy(packet+6*i, macAddress, 6);
	}
}

void uninit()
{
	WSACleanup();
}


void wakeupComputer(char* name)
{
	if ((hostInfo= gethostbyname(name))==NULL)
	{
		errCode=WSAGetLastError();
		printf(&quot;gethostbyname error\n&quot;);
		exit(4);
	}
	memset(&amp;address, 0, sizeof(address));

	//address.sin_addr=AF_INET;
	address.sin_family=hostInfo-&gt;h_addrtype;
	address.sin_port=PortNumber;
	memcpy(&amp;address.sin_addr, hostInfo-&gt;h_addr, hostInfo-&gt;h_length);
	

	if ((errCode=connect(mySocket,(LPSOCKADDR) &amp;address, sizeof(address)))!=0)
	{
		printf(&quot;error of connect\n&quot;);
		exit(3);
	}
	assemblyPacket();
	if ((send(mySocket, packet, MagicPacketLength, 0))==SOCKET_ERROR)
	{
		errCode=WSAGetLastError();
		printf(&quot;send error\n&quot;);
		exit(6);
	}
}

		




int main()
{
	init();
	printf(&quot;%s\n&quot;, &quot;please enter your remote server name :&quot;);
	//cin&gt;&gt;buffer;
	strcpy(buffer, &quot;NICKATI&quot;);

	wakeupComputer(buffer);

	uninit();

	return 0;
}</pre>
</div>
<pre>
</pre>

<pre>¡¡</pre>

<pre><span lang="en-ca">			</span></pre>

<pre><span lang="en-ca">				</span> <a href="game24.htm"><img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp;</p>

</body>

</html>