2016-06-01 159 views
0

我正在嘗試創建一個UDP套接字並將數據發送到廣播端口,以便我可以在同一WiFi網絡中的其他設備上接收相同的數據。我正在計算接受的答案here中給出的廣播端口的IP地址。GCDAsyncUDPSocket:雖然它成功發送,但沒有收到任何數據

之後,我已經寫了一些連接代碼是如下:

self.udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue() socketQueue:nil]; 
NSError *error; 
[self.udpSocket enableReusePort:YES error:&error]; 
[self.udpSocket enableBroadcast:YES error:&error]; 

- (IBAction)sendMessageToBroadcastPort:(id)sender { 
[self.udpSocket sendData:[@"Hi" dataUsingEncoding:NSUTF8StringEncoding] toHost:[self getUDPBroadcastAddress] port:5556 withTimeout:-1 tag:1]; 
} 

我獲得成功發送數據的委託方法didSendData:被調用。

請指導我在這裏錯過了什麼。

謝謝!

UPDATE: Cpde用於從所述廣播端口接收數據:

- (void)listenForPackets 
{ 
dispatch_queue_t dQueue = dispatch_queue_create("client udp socket", NULL); 
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dQueue socketQueue:nil]; 
NSError *error = nil; 

if (![udpSocket bindToPort:5556 error:&error]) { 
    NSLog(@"Error binding: %@",error);//not connecting to host 
    return; 
} 
if (![udpSocket beginReceiving:&error]) { 
    NSLog(@"Error receiving: %@",error); 
    return; 
} 
NSLog(@"Socket Ready"); 
} 

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data 
    fromAddress:(NSData *)address 
withFilterContext:(id)filterContext 
{ 
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
if (msg) 
{ 
    NSLog(@"RCV: %@", msg); 
} 
else 
{ 
    NSString *host = nil; 
    uint16_t port = 0; 
    [GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address]; 
    NSLog(@"Unknown message from : %@:%hu", host, port); 
} 
} 

廣播IP我計算後得到的是192.168.2.255

更新2:

我面對的場景真的不一樣,很奇怪。接收工作有時候並且有時不會。 當我安裝了兩個應用程序時,沒有收到數據。只有發送成功。我保留了應用程序,並在一段時間後應用程序開始接收數據。在某些情況下,它會在一段時間後停止接收,或者一直沒有收到任何問題。可能是什麼問題?

+0

由於UDP連接少,因此不需要調用'connectToHost'。你應該使用'sendData:toHost:port:withTimeout' – Paulw11

回答

0

我回答這個問題,因爲我面對的情況是真的不同,很奇怪。接收工作有時候並且有時不會。 當我安裝了兩個應用程序時,沒有收到數據。只有發送成功。我保留了應用程序,並在一段時間後應用程序開始接收數據。在某些情況下,它會在一段時間後停止接收。

其實我是用我的mac連接到以太網的共享互聯網連接。我改變了我的WiFi網絡到一個合適的寬帶網絡,從那以後它沒有任何問題。

希望這可以幫助有類似情況的人:)

1

嘗試象下面這樣:

創建一個Socket:採用Socket

-(void)createClientUdpSocket 
{ 
    dispatch_queue_t dQueue = dispatch_queue_create("client udp socket", NULL); 

    sendUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue: dQueue socketQueue: nil]; 
    [sendUdpSocket receiveOnce:nil]; 
    [sendUdpSocket joinMulticastGroup:[self getIPAddress] error:nil]; // Put your IP Address 
    NSLog(@"Ready"); 
} 

委託方法。

-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext 
{ 
    NSString *ip = [GCDAsyncUdpSocket hostFromAddress:address]; 
    uint16_t port = [GCDAsyncUdpSocket portFromAddress:address]; 
    NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    // Continue to wait for a message to receive the next 
    NSLog (@ "The server receives the response [%@:% d]%@", ip, port, s); 
    [sock receiveOnce:nil]; 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    [self sendBackToHost:ip port:port withMessage:s]; 
    }); 
} 

-(void)sendBackToHost:(NSString *)ip port:(uint16_t)port withMessage:(NSString *)s{ 

     [sendUdpSocket sendData:yourData toHost:yourIP port:5556 withTimeout:60 tag:200]; 
     NSLog(@"sent message to server"); 
} 

希望這會有所幫助。它爲我工作:)

+0

感謝您的迴應。現在因爲我沒有連接套接字,所以我沒有收到錯誤,但是當我嘗試讀取廣播IP的內容時,示例接收器應用程序沒有收到任何內容。我用接收者的代碼更新了我的問題。或者我的計算IP有問題嗎?請指導。 – Yogi

+0

我已經更新了我的問題,基於我可以設法做到現在。請看一看。 – Yogi

+0

現在正在接收作品,但有問題。請檢查我更新的問題。 – Yogi

相關問題