2

I am currently on XCode 5.0 and working on an iOS 7 sample application which uses CocoaAyncSocket library. In this application, the "sender controller" sends UDP messages on 255.255.255.255 on port 4000 for a "receiver controller" to handle and print out. The "sender controller" has a for loop that broadcasts a message 200 times. Using Wireshark (filtering on udp.port == 4000), of the 200 packets, 0 of them were lost, which is great! In this environment, everything works great, and the "receiver controller" prints out all the messages.

But now when I move the application an actual iPad (iPad MD328LL/A 16GB, Wi-Fi 3rd Generation iOS 7), some of the packets are lost. Of the 200, about 60% - 65% of the packets are picked up by WireShark and make it to the "receiver controller". I am not quite sure if its the library (which I don't think since it works perfectly with the simulator) or and iOS 7/iPad that is causing the packet loss issue.

Code:

// Sender Controller

@interface ViewController ()
{
    GCDAsyncUdpSocket *udpSocket;
}
@end


- (void)viewDidLoad
{
    [super viewDidLoad];

    if (udpSocket == nil)
    {
        [self setupSocket];
    }
    // ...
}

// set ups socket
- (void)setupSocket
{   
    // Initialize
    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    // Enable Broadcast
    [udpSocket enableBroadcast:YES error:nil];

    NSError *error = nil;

    // Bind to port
    if (![udpSocket bindToPort:0 error:&error])
    {
        [self logError:FORMAT(@"Error binding: %@", error)];
        return;
    }
    if (![udpSocket beginReceiving:&error])
    {
        [self logError:FORMAT(@"Error receiving: %@", error)];
        return;
    }

    [self logInfo:@"Ready"];
}

// ...


// Click event
- (IBAction)send:(id)sender
{
    // Format message
    NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];

    // Broadcast message 200 times
    for (int i = 0 ; i < 200; i++) {
        [udpSocket sendData:data toHost:host port:port withTimeout:-1 tag:0];
    }
}

I know that in this scenario these messages are being sent at a fast rate and it would be highly unlikely for a user to send 200 broadcasts like this. I also understand that UDP is cheap and there is a chance of the occasional malformed or lost packets, but at the rate of 40%... that seems rather high to me.

If anybody has any suggestion/experience with this or any useful information, it would be greatly appreciated

Thanks in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mrcolombo
  • 587
  • 4
  • 19
  • I got similar problem. Did you solved your lose packet trouble? My scenario is little bit different with you. My app talks with a hardware with UDP packet. The hardware response really fast, and my app will lose UDP packet. And on iPhone 5s is better than iPhone 4s. On iPhone 5s, lose packet rate is nearly 0%, but on iPhone 4s, I will lose 40% packets. – Yi Jiang Feb 27 '14 at 03:04
  • I never found a solution for this, in my case it was not essential for my app, so I approached it differently... but I would still like to know what is going on. – mrcolombo Feb 27 '14 at 16:12
  • Have you looked into this answer? http://stackoverflow.com/questions/13113435/gcdasyncudpsocket-on-ios-missing-multicasted-datagrams – Captain Fim May 13 '16 at 14:32

0 Answers0