[英]GCDAsyncUDPSocket: Not receiving any data though it is successfully sent
I am trying to create a UDP socket and send data to a broadcasting port so that I can receive the same on other devices in the same WiFi network. 我正在尝试创建UDP套接字并将数据发送到广播端口,以便可以在同一WiFi网络中的其他设备上接收到相同的消息。 I am calculating the IP address of broadcasting port as given in the accepted answer here .
我正在按此处接受的答案计算广播端口的IP地址。
After that I have written some connection code which is as below: 之后,我编写了一些连接代码,如下所示:
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];
}
I get success in sending the data as the delegate method didSendData: gets called. 当委托方法didSendData:被调用时,我成功发送了数据。
Please guide me on what am I missing here. 请指导我我在这里想念的东西。
Thanks! 谢谢!
UPDATE: Cpde for receiving the data from the broadcasting port: 更新: 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);
}
}
The broadcasting IP which I get after calculation is 192.168.2.255 经过计算得到的广播IP是192.168.2.255
UPDATE 2: : 更新2 ::
The scenario I am facing is really different and weird. 我面临的情况确实是不同且奇怪的。 The receiving works sometimes and sometimes it doesn't.
接收有时有效,有时无效。 When I installed two apps there were no data received.
当我安装两个应用程序时,没有收到数据。 Only sending was successful.
仅发送成功。 I kept the apps on and after some time the app started receiving data.
我将应用程序保持打开状态,一段时间后,该应用程序开始接收数据。 At some instances it stopped receiving after some time or kept receiving without any issue.
在某些情况下,它会在一段时间后停止接收或继续接收而没有任何问题。 What can be the issue?
可能是什么问题?
Try like Below : 尝试如下:
Create a 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");
}
Delegate method for Socket. Socket的委托方法。
-(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");
}
Hope this will help. 希望这会有所帮助。 Its working for me :)
它为我工作:)
I am answering this question as the scenario I faced is really different and weird. 我正在回答这个问题,因为我所面对的情况确实不同且奇怪。 The receiving works sometimes and sometimes it doesn't.
接收有时有效,有时无效。 When I installed two apps there were no data received.
当我安装两个应用程序时,没有收到数据。 Only sending was successful.
仅发送成功。 I kept the apps on and after some time the app started receiving data.
我将应用程序保持打开状态,一段时间后,该应用程序开始接收数据。 At some instances it stopped receiving after some time.
在某些情况下,一段时间后它停止接收。
Actually I was using a shared internet connection of my mac which is connected to ethernet. 实际上,我正在使用连接到以太网的Mac的共享Internet连接。 I changed my WiFi network to a proper broadband network and since then it works without any issue.
我将WiFi网络更改为适当的宽带网络,此后它就可以正常工作了。
Hope this helps someone with similar scenario :) 希望这可以帮助类似情况的人:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.