iOS devices not receiving UDP multicast using GDAsyncUdpSocket

為{幸葍}努か 提交于 2019-12-19 09:18:23

问题


The code below is intended to receive UDP multicast messages on 239.255.255.250 and simply NSLog the contents of the message.

If I address a message to the IP of the iOS device (i.e. from a terminal echo foo | nc -u 10.1.10.249 1900) the message is received and NSLog'd.

However, if I broadcast a message to the multicast address (echo bar | nc -u 239.255.255.250 1900), the message is not received.

No error messages are logged at start up.

Thoughts on where I'm going awry?

#import "ViewController.h"
#import "GCDAsyncUdpSocket.h"

@interface ViewController () {
    GCDAsyncUdpSocket *udpSocket;
}
@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError *error = nil;

    if (![udpSocket bindToPort:1900 error:&error]) {
        NSLog(@"Error starting server (bind): %@", error.description );
        return;
    }

    if(![udpSocket joinMulticastGroup:@"239.255.255.250" error:&error] ) { //]onInterface:@"en0" error:&error]) {
        NSLog(@"Error joining multicast group: %@",error.description);
        return;
    }

    if (![udpSocket beginReceiving:&error]) {
        [udpSocket close];
        NSLog(@"Error starting server (recv): %@", error.description);
        return;
    }

    NSLog(@"Udp server started on port %@:%hu", [udpSocket localHost_IPv4], [udpSocket localPort]);
}

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"message rec'd: %@:%hu %@\n", [udpSocket localHost_IPv4], [udpSocket localPort],msg);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

回答1:


You're missing a key function that stumped me for a time, too.

[udpSocket enableBroadcast:YES error:&error];

This will allow you to send broadcast packets and receive broadcasted packets from your multicast group.



来源:https://stackoverflow.com/questions/24393371/ios-devices-not-receiving-udp-multicast-using-gdasyncudpsocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!