Remote Wake-Up on LAN)

Source: Internet
Author: User

// Wake up on LAN-send a magic packet to a MAC address
// Magic packet: UDP broadcast package, unlimited port, data is FF-FF-FF-FF-FF-FF and 16 Mac
// Compile: CL sendmagic. cpp ws2_32.lib

# Include <windows. h>
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
// Check command line parameters
If (argc! = 2)
{
Printf ("Wakeup Your PC on LAN! By CJ <4uto@163.com>/N ");
Printf ("Usage: % S <MAC address>/N", argv [0]);
Printf ("Example: % s 00-d0-4c-bf-52-ba", argv [0]);
Return 0;
}

// Check whether the MAC address is correct
For (char * A = argv [1]; * A; A ++)
If (*! = '-'&&! Isxdigit (* ))
{
Fprintf (stderr, "Mac adresse must be like this: 00-d0-4c-bf-52-ba ");
Return 1;
}

Int dstaddr [6];
Int I = sscanf (argv [1], "% 2X-% 2X-% 2X-% 2X-% 2X-% 2x ",
& Dstaddr [0], & dstaddr [1], & dstaddr [2], & dstaddr [3], & dstaddr [4], & dstaddr [5]);
If (I! = 6)
{
Fprintf (stderr, "invalid Mac adresse! ");
Return 1;
}

Unsigned char ether_addr [6];
For (I = 0; I <6; I ++)
Ether_addr [I] = dstaddr [I];

// Construct Magic Packet
U_char magicpacket [200];
Memset (magicpacket, 0xff, 6 );
Int packetsize = 6;
For (I = 0; I <16; I ++)
{
Memcpy (magicpacket + packetsize, ether_addr, 6 );
Packetsize + = 6;
}

// Start WSA
Wsadata;
If (wsastartup (makeword (2, 0), & wsadata )! = 0)
{
Fprintf (stderr, "wsastartup failed: % d/N", getlasterror ());
Return 1;
}

// Create a socket
Socket sock = socket (af_inet, sock_dgram, 0 );
If (sock = invalid_socket)
{
Fprintf (stderr, "socket create error: % d/N", getlasterror ());
Return 1;
}

// Set to broadcast transmission
Bool boptval = true;
Int ioptlen = sizeof (bool );
If (setsockopt (sock, sol_socket, so_broadcast, (char *) & boptval, ioptlen) = socket_error)
{
Fprintf (stderr, "setsockopt error: % d/N", wsagetlasterror ());
Closesocket (sock );
Wsacleanup ();
Return 1;
}

Sockaddr_in;
To. sin_family = af_inet;
To. sin_port = htons (0 );
To. sin_addr.s_addr = htonl (inaddr_broadcast );

// Send magic Packet
If (sendto (sock, (const char *) magicpacket, packetsize, 0, (const struct sockaddr *) & to, sizeof (to) = socket_error)
Fprintf (stderr, "Magic packet send error: % d", wsagetlasterror ());
Else
Printf ("Magic packet send! ");

Closesocket (sock );
Wsacleanup ();
Return 0;
}

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.