Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have used AsyncUdpSocket successfully to run SSDP Discovery and find controllers. Here are my code snippets:</p> <p>Initialize and setup the socket:</p> <pre><code>// AsyncUdpSocket *ssdpSock = [[AsyncUdpSocket alloc] initWithDelegate:self]; AsyncUdpSocket *ssdpSock = [[AsyncUdpSocket alloc] initIPv4]; [ssdpSock setDelegate:self]; </code></pre> <p>Note the first line commented out. I found on the <a href="http://groups.google.com/group/cocoaasyncsocket/msg/8b924d6d0ae4e4f3" rel="nofollow">AsyncUdpSocket forums</a> some issues with duplicates. I don't think I was facing them but I did it anyhow.</p> <p>I added error checking, and it was useful because during my debugging I wasn't closing sockets and I started getting socket setup failures:</p> <pre><code>NSError *socketError = nil; if (![ssdpSock bindToPort:1900 error:&amp;socketError]) { NSLog(@"Failed binding socket: %@", [socketError localizedDescription]); return statusController; } if(![ssdpSock joinMulticastGroup:@"239.255.255.250" error:&amp;socketError]){ NSLog(@"Failed joining multicast group: %@", [socketError localizedDescription]); return statusController; } if (![ssdpSock enableBroadcast:TRUE error:&amp;socketError]){ NSLog(@"Failed enabling broadcast: %@", [socketError localizedDescription]); return statusController; } [ssdpSock sendData:[self.discoverControllerString dataUsingEncoding:NSUTF8StringEncoding] toHost:@"239.255.255.250" port:1900 withTimeout:2 tag:1]; </code></pre> <p>Notice the changes I have made to the time out. And then finally did the receive setup, and closed the socket. Note the socket close. Since I am in my own class when I am running this - the code above did not work for me.</p> <pre><code>[ssdpSock receiveWithTimeout: 2 tag:1]; [NSTimer scheduledTimerWithTimeInterval: 5 target: self selector:@selector(completeSearch:) userInfo: self repeats: NO]; [ssdpSock closeAfterSendingAndReceiving]; </code></pre> <p>The most important change probably was returning "NO" if I did not find my controller. The first receive was incidentally the discovery message itself coming back. And when I read through the AsyncUdpSocket.h file carefully - returning "NO" when it is not a packet you are looking for helped.</p> <p>Also note that I am using ARC in my code but I compiled the AsyncUdpSocket without ARC support.</p> <pre><code>-(void) completeSearch: (NSTimer *)t { NSLog(@"%s",__FUNCTION__); //[ssdpSock close]; //ssdpSock = nil; } - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port { NSLog(@"%s %ld %@ %d",__FUNCTION__,tag,host,port); NSString *aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"%@",aStr); NSString *compareString = [aStr stringByPaddingToLength:[self.responseString length] withString:@"." startingAtIndex:0]; //NSLog(@"%@", compareString); //NSLog(@"%@", self.responseString); if ([compareString isEqualToString:self.responseString]) { NSLog(@"String Compare, Controller Found!"); [self.controllerList addObject:aStr]; //NSData *controllerIP = [aStr dataUsingEncoding:NSUTF8StringEncoding]; [[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoveredController" object:nil]; return YES; } return NO; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload