en0 seems to be the wifi interface for Apple iOS devices, but in my code, small multicast client when I specify the interface en0 I'm not receiving anything. Any clue of what could be wrong ? GDCasyncUdpSocket logs don't show any error => Binding socket to port(1234) interface((en0))
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//log test
[DDLog addLogger:[DDTTYLogger sharedInstance]];
// Create multicast High Priotity queue
mcastQueue = dispatch_queue_create("mcastQueue", NULL);
dispatch_queue_t high = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_set_target_queue(mcastQueue, high);
// Create UDP Socket
mcastSocket=[[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:mcastQueue];
[mcastSocket setPreferIPv4];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSError *socketError=nil;
if (![mcastSocket bindToPort:1234 interface:@"en0" error:&socketError]) {
NSLog(@"Failed binding socket to port: %@" ,socketError);
return;
}
if (![mcastSocket enableBroadcast:YES error:&socketError]) {
NSLog(@"Failed enabling broadcast: %@" ,socketError);
return;
}
if (![mcastSocket joinMulticastGroup:@"239.0.0.1" error:&socketError]) {
NSLog(@"Failed joining multicast group: %@" ,socketError);
return;
}
//start receiving multicast data
if (![mcastSocket beginReceiving:&socketError]) {
[mcastSocket close];
NSLog(@"Failed to start receiving: %@" ,socketError);
} else {
NSLog(@"Multicast start receiving");
}
}
Regards
After a year or so of intermittently trying to solve this and giving up, I finally discovered what I believe is the answer to this.
The answer is that you should not bind to an interface when receiving UDP packets. (See https://stackoverflow.com/a/10739443/179216)
Instead you should specify the interface when joining the multicast group:
// Do *not* specify the interface here
if (![mcastSocket bindToPort:1234 error:&socketError]) {
NSLog(@"Failed binding socket to port: %@" ,socketError);
return;
}
if (![mcastSocket enableBroadcast:YES error:&socketError]) {
NSLog(@"Failed enabling broadcast: %@" ,socketError);
return;
}
// This is where you specify the interface
if (![mcastSocket joinMulticastGroup:@"239.0.0.1" onInterface:@"en0" error:&socketError]) {
NSLog(@"Failed joining multicast group: %@" ,socketError);
return;
}