2012-08-13 92 views
2

en0似乎是蘋果iOS設備的wifi接口,但在我的代碼中,當我指定接口en0時,小型多點傳送客戶端我沒有收到任何東西。任何可能出錯的線索? GDCasyncUdpSocket日誌不顯示任何錯誤=> 綁定插座端口(1234)接口((EN0))GCDasyncUdpSocket和wifi接口

- (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"); 
} 
} 

問候

+0

太少代碼什麼都看不到。 en0確實是wifi接口。您應該顯示委託方法的作用,以及mcastSocket的創建。 – DarthMike 2012-08-13 14:36:59

+0

'socketError'中有什麼細節? – Joe 2012-08-13 14:58:58

+0

嗨DarthMike,我已經添加了mcastSocket創建。如果您不指定接口,代碼工作正常。 – tony590 2012-08-13 15:37:49

回答

1

經過一年左右的間歇試圖解決這一點,並放棄,我終於發現我相信這是答案。

答案是你應該不是在接收UDP數據包時綁定到接口。 (見https://stackoverflow.com/a/10739443/179216

相反,你應該指定接口時加入組播組:

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