iOS devices not receiving UDP multicast using GDAsyncUdpSocket(iOS 设备未使用 GDAsyncUdpSocket 接收 UDP 多播)
问题描述
下面的代码旨在在 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'd.
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.
想好我要去哪里了吗?
#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 %@
", [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 多播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 设备未使用 GDAsyncUdpSocket 接收 UDP 多播
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01