iossocketsudpgcdasyncsocketgcdasyncudpsocket

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. I am calculating the IP address of broadcasting port as given in the accepted answer here.

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.

Please guide me on what am I missing here.

Thanks!

UPDATE: Cpde for receiving the data from the broadcasting port:

- (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

UPDATE 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?


Solution

  • 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. I changed my WiFi network to a proper broadband network and since then it works without any issue.

    Hope this helps someone with similar scenario :)