iOS设备未使用GDAsyncUdpSocket接收UDP多播 [英] iOS devices not receiving UDP multicast using GDAsyncUdpSocket
问题描述
下面的代码旨在接收239.255.255.250上的UDP多播消息,并简单地NSLog消息的内容.
The code below is intended to receive UDP multicast messages on 239.255.255.250 and simply NSLog the contents of the message.
如果我将消息发送到iOS设备的IP地址(即从终端echo foo | nc -u 10.1.10.249 1900
发送),则会收到消息并进行NSLog处理.
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.
但是,如果我将消息广播到多播地址(echo bar | nc -u 239.255.255.250 1900
),则不会收到该消息.
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
推荐答案
您也缺少一次让我感到困扰的关键功能.
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.
这篇关于iOS设备未使用GDAsyncUdpSocket接收UDP多播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!