简体   繁体   English

如何通过UDP CFSocket发送数据包?

[英]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). 我是一个完整的网络新手,但我是ac / c ++程序员,我正在使用objective-c(这是针对OSX / iPhone)。

I am trying to learn how to send a magic packet with a UDP socket using cfsocket. 我正在尝试学习如何使用cfsocket发送带有UDP套接字的魔术包。 I've seen there are libraries such as AsyncUDPSocket library, however I do not want to use these. 我已经看到有像AsyncUDPSocket库这样的库,但是我不想使用它们。

I attempted to look at apples UDPecho file but, as a beginner it did confuse me. 我试图查看苹果UDPecho文件,但作为一个初学者它确实让我困惑。 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. 我用Google搜索了ALOT,我已经把下面的代码放在一起,我有一个数据包嗅探器运行,它没有注册任何被发送的东西。 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? (道歉,如果这种接缝含糊不清)我的意思是我错过了代码的某些阶段,例如: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. 您的问题非常简单:您没有告诉CFSocket您希望它发送数据的位置。

You carefully construct an address in addr , but then don't use it. 您在addr仔细构造一个地址,但之后不要使用它。 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 . 您需要调用CFSocketSetAddress来设置套接字上的目标地址(如果您希望发送的所有数据包都转到同一目的地),或者将该地址作为第二个参数传递给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. 我禁用了listen调用,因为我只想发送数据。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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