Digonto
Digonto

Reputation: 25

Writing Magic Packet in C++

I want to send magic packet to wake up a specific PC(specific MAC address) connected to an mbed through LAN . I have found the following code in code project.

My MAC Address : 00-C0-9F-BD-E4-3A (sample).....how can i declare this and fill this in the array.* I have little knowledge in Programming. so please ignore any mistake or miss interpretation.

BYTE magicP[102];

...

//Fill in magic packet with 102 Bytes of data

//Header

//fill 6 Bytes with 0xFF

for (int i=0;i<6;i++)
    magicP[i] = 0xff;
//First 6 bytes (these must be repeated!!)

//fill bytes 6-12

for (i=0;i<6;i++) {
    //Get 2 charachters from mac address and convert it to int to fill

    //magic packet

    magicP[i+6] = HexStrToInt(macAddr.Mid(i*2,2));
}

//fill remaining 90 bytes (15 time repeat)

for (i=0;i<15;i++) memcpy(&magicP[(i+2)*6],&magicP[6],6);

...

here is the like where i have found this code: http://www.codeproject.com/KB/IP/WOL.aspx

Upvotes: 0

Views: 5516

Answers (1)

Lars
Lars

Reputation: 1387

I'm afraid to you are not providing enough of the code to give an unambiguous answer (a link might help), but I think that you don't have to fill in magicP[] yourself - this is what the code is doing for you. It appears that you have to provide your hexadecimal MAC address as input in the macAddr variable, as a string of 12 hexadecimal digits (e.g. "023FDCA889FA").

Edit: After reading the link, I consider my hunch confirmed. The code snippet shows how the magicP[] array (which eventually holds the packet payload) is constructed based on the provided macAddr, in order to illustrate a theoretical explanation preceding it. So somewhere there needs to be a definition std::string macAddr; into which you enter your MAC address, and which is then passed to the calculation method.

I suggest that you download the full source from the linked project (or some other open WOL program), as that will give a better understanding on how the pieces fit together.

Upvotes: 1

Related Questions