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!