Waking computers from sleep...

This is a discussion on Waking computers from sleep... within the Windows Programming forums, part of the Platform Specific Boards category; New member... fwiw. I've been searching like crazy for a solution to a bizarre problem I'm having with some windows ...

  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

    Waking computers from sleep...

    New member... fwiw.

    I've been searching like crazy for a solution to a bizarre problem I'm having with some windows networking...

    The networking part is going fine. I'm using UDP to send commands from maching to machine on my lan and so far so good... Everyone's getting along nicely... so long as the machines are awake, my little setup works perfectly. Where the problem comes in is when the host machine is in sleep or hibernate mode...

    On Windows 7 the machine wakes up from the first DNS inquiry (getting it's IP from it's name), subsequent inquiries are buffered and do not wake it up. On XP DNS does not wake the machine but the first command packet I send wakes it up (and gets lost). Magic packets are not an option since I can't get the MAC address while the machine is asleep and there's no guarantee of any one machine always being at the same IP if I do, so databasing them is fruitless...

    The problem is these machines have to wake up when a stream of command packets begins arriving. I can send pings and wait for a response... but half the time the response never comes.

    So... comes the question... Is there a universal way to wake a machine?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Kind of a stumper... ain't it?

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Is there a universal way to wake a machine?
    In theory, there is an authoritive force in your network that gave an IP address to this machine. It probably stored the mac address while doing this. So you can send a magic packet if you query for a mac by ip. You just have to find out how to execute this query.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nvoigt View Post
    In theory, there is an authoritive force in your network that gave an IP address to this machine. It probably stored the mac address while doing this. So you can send a magic packet if you query for a mac by ip. You just have to find out how to execute this query.
    Thank you for your response...

    In normal situations, this is handled by winapi calls such as SendARP. Basically you hand over the machine's IP and get it's 6 byte mac address in response. However... In Win7 if the machine is already asleep, SendARP (etc.) will fail because it's in standby. You end up having to go over to the machine (which could be in a separate building) and hit the space bar... You can't get the info to wake the machine, because it's asleep.

    I know these things can be databased and some local buffering is done by the system... But I've yet to find a way to get an authoritive response for a machine that has (for example) been hooked to the net and let fall asleep before I get to it...

    In Vista/XP/2000 this was easy... just send a blank datagram to an open port and voila it wakes up! For Win7 they decided to reinvent this extremely simple protocal, making it a royal pain in the butt.

    The details are here:
    Power Management for Network Devices in Windows 7

    Now I'm not a proud person when it comes to software. I know half of these problems are due to something I'm either misunderstanding or doing wrong... but I swear I've tried everything in this case and just can't get a Win7 machine to wake up reliably when I need to send it a string of datagrams.

    FWIW... The application is a simple audio intercom that sends wave data by UDP. When an operator clicks on a host name, I need to wake that computer up before sending and give the operators some visual indication when to start talking...
    Last edited by CommonTater; 08-23-2010 at 12:35 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I may have had a bit of a breakthrough here...

    This appears to work...
    What I did different was to send the magic packet to a scrap port (9) instead of the open port for my software.

    Code:
    // build and send magic packet
    VOID WakeHost(PSOCKADDR Host)
      { SOCKADDR  ha;                 // host ip and port
        BYTE      mac[6];             // host mac address 
        ULONG     smac = 6;           // size of mac address
        BYTE      magic[128] = {0};   // magic packet  
        // make a copy of the sockaddr
        memcpy(&ha,Host,sizeof(SOCKADDR));
        // switch port 
        SetHostPort(9,&ha);
        // get mac address
        SendARP(((PSOCKADDR_IN)&ha)->sin_addr.S_un.S_addr,0,(PULONG)&mac,&smac);
        // build magic packet
        memset(&magic,255,6);
        for ( int x = 6; x < 102 ; x++)
           magic[x] = mac[x % 6];
        // send to host computer
        sendto(hSocket,(PCHAR)&magic,128,0,&ha,sizeof(SOCKADDR)); }
    It's woken my win7 machines up about a dozen times so far.
    Testing on XP is pending access to a couple of machines, this evening.
    Last edited by CommonTater; 08-23-2010 at 04:38 PM.

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    OS shouldn't matter. Wake-On-LAN(along with all forms of waking) is handled by the BIOS.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by User Name: View Post
    OS shouldn't matter. Wake-On-LAN(along with all forms of waking) is handled by the BIOS.
    Actually wake from power off is handled in the BIOS.
    Wake from sleep is an OS function. (see my comments and Microsoft's descriptions, above)

    I don't think I want to wake from power off for this application.
    Last edited by CommonTater; 08-24-2010 at 10:48 AM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For what it's worth... The fixed code I published above does seem to work on everything from Win XP SP2 and up.

    If anybody tries it for themselves, I'd appreciate any feedback...

    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numeric addresses for computers
    By great in forum C Programming
    Replies: 4
    Last Post: 08-23-2010, 12:53 PM
  2. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  3. Computers as authors
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-22-2004, 08:55 PM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 09:21 AM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21