Dispositivos iOS que no reciben multidifusión UDP usando GDAsyncUdpSocket
El siguiente código está destinado a recibir mensajes de multidifusión UDP en 239.255.255.250 y simplemente NSLog el contenido del mensaje.
Si dirijo un mensaje a la IP del dispositivo iOS (es decir, desde un terminalecho foo | nc -u 10.1.10.249 1900
) el mensaje es recibido y NSLog'd.
Sin embargo, si transmito un mensaje a la dirección de multidifusión (echo bar | nc -u 239.255.255.250 1900
), el mensaje no se recibe.
No se registran mensajes de error al inicio.
¿Pensamientos sobre dónde voy mal?
#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