How to send a packet via a UDP CFSocket?

Viewed 8020

I am a complete newbie to networking, however I am a c/c++ programmer, and am working in objective-c (This is for OSX/iPhone).

I am trying to learn how to send a magic packet with a UDP socket using cfsocket. I've seen there are libraries such as AsyncUDPSocket library, however I do not want to use these.

I attempted to look at apples UDPecho file but, as a beginner it did confuse me. I've googled around ALOT, and I have put together the code below, I have a packet sniffer running and it doesn't register anything being sent. I understand my code is missing alot of error catching, however I am just trying to get the basics in first.

Is this code right? (apologies if this seams ambiguous) What I mean is am I missing certain stages of the code such as: CFSocketSetAddress?

If anyone knows any really good tutorials it would help.

Any help is greatly appreciated, as I am a complete beginner at this.

Thanks

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CFSocketRef WOLsocket;
    WOLsocket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, NULL, NULL);
    if ( socket == NULL) {
        NSLog(@"CfSocketCreate Failed");
    }else{
        if( socket ) {
            NSLog(@"Socket created :)");

                struct sockaddr_in addr;
                memset(&addr, 0, sizeof(addr));
                addr.sin_len = sizeof(addr);
                addr.sin_family = AF_INET;
                addr.sin_port = htons(9); //port
                inet_aton("255.255.255.255", &addr.sin_addr); //ip adress


                char ethadd []= "helloworld";
                CFDataRef Data = CFDataCreate(NULL, (const UInt8*)ethadd, sizeof(ethadd));
                CFSocketSendData(socket,NULL, Data, 0);}

    }

    [pool drain];
    return 0;
}
2 Answers

This is my first working bit of code for two whole nights! Hopefully will help someone else! Thought I should share it, might save someone a bit of hassle.

I disabled the listen call, as I only wanted to send data.

+ (void)broadCast
{
    int socketSD = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (socketSD <= 0) {
        NSLog(@"Error: Could not open socket.");
        return;
    }

    // set socket options enable broadcast
    int broadcastEnable = 1;
    int ret = setsockopt(socketSD, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
    if (ret) {
        NSLog(@"Error: Could not open set socket to broadcast mode");
        close(socketSD);
        return;
    }

    // Configure the port and ip we want to send to
    struct sockaddr_in broadcastAddr;
    memset(&broadcastAddr, 0, sizeof(broadcastAddr));
    broadcastAddr.sin_family = AF_INET;
    inet_pton(AF_INET, "255.255.255.255", &broadcastAddr.sin_addr);
    broadcastAddr.sin_port = htons(33333);

    char *request = "message from ios by c";
    ret = sendto(socketSD, request, strlen(request), 0, (struct sockaddr*)&broadcastAddr, sizeof(broadcastAddr));
    if (ret < 0) {
        NSLog(@"Error: Could not open send broadcast.");
        close(socketSD);
        return;
    }

    close(socketSD);

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //[self listenForPackets];
    });

}
Related