Trying send wake on lan, program faults out [SOLVED]

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Trying send wake on lan, program faults out [SOLVED]

Post by Tony0945 » Sat Jul 25, 2015 9:35 pm

I'm just trying to send the wake on lan packet to a selected mac address one time. But when it runs, the program faults.

Code: Select all

void wakeonlanDlg::SendMagicPacket(wxLongLong mac)
{
	unsigned char buffer[BUFFER_SIZE];
	int i;
	
	wxIPV4address LocalAddress;
    LocalAddress.AnyAddress(); 
    LocalAddress.Hostname(_("255.255.255.255"));
    LocalAddress.Service(7);
	socket = new wxDatagramSocket(LocalAddress);
	socket->Initialize();
	
    // fill the buffer
    for (i=0;i<6;++i) buffer[i]=0xFF; // six bytes of 0xFF
    for (int n=0; n<16;++n)
    {
        unsigned char byte;
        for (int k=0;k<6;++k)
        {
            byte = mac.GetLo() & 0xFF;
            // shift mac to the right
            mac >>= 8;
            buffer[i++]= byte;  //extract by position
        }
    }
    
    // send the packet

    socket->SendTo(LocalAddress,buffer,BUFFER_SIZE);
}
The error message is:
../src/common/socket.cpp(2073):assert in "m_impl" failed in SendTo(): socket not initialised

How can I debug this? I know next to nothing about sockets. I was just trying to follow the examples.
Last edited by Tony0945 on Mon Jul 27, 2015 12:07 am, edited 1 time in total.

User avatar
doublemax
Moderator
Moderator
Posts: 14511
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Trying send wake on lan, program faults out

Post by doublemax » Sat Jul 25, 2015 9:48 pm

Try calling wxSocket::Initialize() once, e.g. in wxApp::OnInit()
Use the source, Luke!

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: Trying send wake on lan, program faults out

Post by Tony0945 » Sat Jul 25, 2015 10:55 pm

It wouldn't compile until I made it 'wxSocketBase::Initialize'. Then it compiled but had the same run time fault.

User avatar
doublemax
Moderator
Moderator
Posts: 14511
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Trying send wake on lan, program faults out

Post by doublemax » Sat Jul 25, 2015 11:39 pm

It wouldn't compile until I made it 'wxSocketBase::Initialize'.
Yes, sorry about that. Unless that code is in a separate thread, this call is probably not needed anyway.

Apparently creating the UDP socket fails, you should check with IsOk() before using it.

I'm not familiar with socket programming either, but your code looks a little different than the code from the "sockets" sample. Maybe try to use the code from there.
Use the source, Luke!

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: Trying send wake on lan, program faults out

Post by Tony0945 » Mon Jul 27, 2015 12:07 am

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.

Post Reply