简体   繁体   中英

How to send a packet via a UDP CFSocket?

I am a complete newbie to networking, however I am ac/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;
}

Your problem is very simple: you are not telling the CFSocket where you want it to send the data.

You carefully construct an address in addr , but then don't use it. You either need to call CFSocketSetAddress to set the destination address on the socket (if you want all packets you send to go to the same destination), or pass the address as the 2nd argument to CFSocketSendData .

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];
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM