سؤال

I am making a simple WOL application. So far I can successfully create a socket, however when setting the address using CFSocketSetAddress I can only use the network ip of my computer (WiFi ip = 192.168.0.5) or local ip (127.0.0.1).

For WOL I would like to send the data to the broadcast address (255.255.255.255). If this is entered I am returned with the error 'address could not be set'.

Am I miss-understanding the use of CFSocketSetAddress, and the address is supposed to be the hosts IP, or the destination IP? In either case, what do I need to do, so that my destination ip is the broadcast address?

Below is some of my code:

        /*************************************************************************/


        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_len = sizeof(addr);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT); //port
        inet_aton(IP, &addr.sin_addr);//IP is the host network ip: 192.168.0.5 


        NSData *address = [NSData dataWithBytes: &addr length: sizeof(addr)];

        if (CFSocketSetAddress(WOLsocket, (CFDataRef)address) != kCFSocketSuccess){
            NSLog(@"Address could not be set!");
       } 
هل كانت مفيدة؟

المحلول

I solved my problem by using a different method (Got native socket), and then instead of using CFSocketSetAddress, I passed the address in the second argument of CFSocketSendData.

I don't have a link/reference for the changes, as it was some code stored on my HDD from days of heavy googling.

Would like to say thanks to David Gelhar Who pointed me in the right direction from my previous question.

for anyone else who might need this this is my code;

//Gets native & sets options
        /*************************************************************************/
        int desc = -1;
        desc = CFSocketGetNative(WOLsocket);
        int yes = 1;

        if (setsockopt (desc, SOL_SOCKET, SO_BROADCAST, (char *)&yes, sizeof (yes)) < 0) {
            NSLog(@"Set Socket options failed");
            return EXIT_FAILURE;
        }
        //sets address socket - doesn't bind this is done in CFSocketSendData
        /*************************************************************************/
        unsigned long bcast = 0xffffffff;

        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_len = sizeof(addr);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT); //port
        addr.sin_addr.s_addr = bcast;
        NSData *address = [NSData dataWithBytes: &addr length: sizeof(addr)];

        //Sends Data
        /*************************************************************************/
        char ethadd []= "helloworld";
        CFDataRef Data = CFDataCreate(NULL, (const UInt8*)ethadd, sizeof(ethadd));
        if (CFSocketSendData(WOLsocket, address, Data, 0) < 0){
            NSLog(@"Data could not be sent!");
            return EXIT_FAILURE;
        }
        else NSLog(@"Data Sent");
    }

نصائح أخرى

For my app, the problem is that it was sandboxed, but it wasn't given network permissions.

If you're using App Sandbox, you need to check one of these:

The App Sandbox section of a project's settings, with 'Incoming Connections (Server)' checked

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top