2016-06-01 32 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

相关问题