gpt4 book ai didi

iphone - GCDasyncUdpSocket 和 wifi 接口(interface)

转载 作者:行者123 更新时间:2023-12-03 16:43:01 27 4
gpt4 key购买 nike

en0 似乎是 Apple iOS 设备的 wifi 接口(interface),但在我的代码中,小型多播客户端当我指定接口(interface) en0 时,我没有收到任何内容。有什么可能出问题的线索吗? GDCasyncUdpSocket 日志不显示任何错误 =>将套接字绑定(bind)到端口(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");
}
}

问候

最佳答案

经过一年左右的间歇性尝试解决这个问题并放弃后,我终于找到了我认为的答案。

答案是,在接收 UDP 数据包时,您不应该绑定(bind)到接口(interface)。 (参见https://stackoverflow.com/a/10739443/179216)

相反,您应该在加入多播组时指定接口(interface):

// 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;
}

关于iphone - GCDasyncUdpSocket 和 wifi 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11936267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com