IOS devices not receiving UDP multicast using GDAsyncUdpSocket

The code below is for receiving UDP multicast messages at 239.255.255.250 and just NSLog message content.

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

However, if I send the message to the multicast address ( echo bar | nc -u 239.255.255.250 1900), the message will not be received.

Error messages are not logged at startup.

Thoughts on where I will be wrong?

#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
+4
source share
1 answer

You miss the key feature that puzzled me too much.

[udpSocket enableBroadcast:YES error:&error];

.

+2

Source: https://habr.com/ru/post/1545885/


All Articles