I found an identical post in German from 2009. Nobody answered it. I have found the answers and here they are.
The Initialize call is not needed for a single threaded operation.
One problem was sending to LocalAddress. Once I set .Hostname to the hostname, Windows Firewall popped up a box asking if I wanted to open the port. Good News, I'm actually sending something. But it turns out the name LocalAddress taken from a wiki example is a poor name. It's supposed to be the adress we are sending from, not the address we are sending to. Once I fixed this by added the object BroadcastAddress, the error messages ceased. But I still couldn't see anything from Wireshark. Then I got an inspiration. With Wireshark still running I ran a different perl based WOL program. Sure enough Wireshark saw it and said it was a Magicpacket in WOL format. However, everything that I had read said the port should be either 7 or 9. This working perl program transmitted from port 1681 to remote port 40000. I changed to those values and Wireshark saw my WOL transmission. But the data was screwed up. I fixed that and now the program works. The Initialization code is:
Code: Select all
LocalAddress.AnyAddress();
LocalAddress.Service(1681);
BroadcastAddress.Hostname(_("255.255.255.255"));
BroadcastAddress.Service(40000);
socket = new wxDatagramSocket(LocalAddress,wxSOCKET_BROADCAST|wxSOCKET_WAITALL);
if (!socket) wxMessageBox("punt");
LocalAddress and BroadcastAddress are objects of class wxIPv4address.
The transmission code is:
Code: Select all
void wakeonlanDlg::SendMagicPacket(wxLongLong mac)
{
unsigned char buffer[BUFFER_SIZE]; //transmission buffer of size 144
int i;
// first translate mac into a stream of bytes
unsigned char mbuffer[6];
mbuffer[0] = (mac.GetHi() & 0xFFFF) >>8;
mbuffer[1] = (mac.GetHi() & 0xFF);
mbuffer[2] = (mac.GetLo() & 0xFF000000) >>24;
mbuffer[3] = (mac.GetLo() & 0x00FF0000) >>16;
mbuffer[4] = (mac.GetLo() & 0x0000FF00) >>8;
mbuffer[5] = (mac.GetLo() & 0x000000FF);
// fill the buffer
for (i=0;i<6;++i) buffer[i]=0xFF; // six bytes of 0xFF
for (int n=0; n<16;++n) //sixteen copies of the mac address
{
for (int j=0;j<6;++j) // six bytes
buffer[i++]= mbuffer[j]; //extract by position
}
if (i!=144) wxMessageBox("oops");
// send the packet
if (!socket->IsInitialized())
{
wxMessageBox("Not Initialized");
return;
}
if (socket->IsOk())
{
socket->SendTo(BroadcastAddress,buffer,BUFFER_SIZE);
if (socket->Error())
{
wxMessageBox("Transmission Error");
}
}
else wxMessageBox("Socket is honked up!");
}
The socket creation flags are very important.
Thank you, Doublemax. You got me thinking which is always a good thing.