GCDAsyncSocket Multiple Connections?

Ya estoy usando GCDAsyncSocket para conectar 2 dispositivos. Una transmisión por sí misma y aceptación de connection, la otra escucha y request de connection. Si bash conectar otro dispositivo al host que aún emite, se conecta y obtiene una terminación de connection mientras el primer dispositivo aún está conectado.

¿Cómo puedo reestructurar mi código para aceptar múltiples conexiones? ¿Qué me estoy perdiendo? ¡Por favor ayuda, estoy tratando de resolver esto! Aquí está mi código:

- (void)startBroadcast { // Initialize GCDAsyncSocket self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; // Start Listening for Incoming Connections NSError *error = nil; if ([self.socket acceptOnPort:0 error:&error]) { // Initialize Service self.service = [[NSNetService alloc] initWithDomain:@"local." type:@"_iQuest._tcp." name:@"" port:[self.socket localPort]]; // Configure Service [self.service setDelegate:self]; // Publish Service [self.service publish]; } else { NSLog(@"Unable to create socket. Error %@ with user info %@.", error, [error userInfo]); } } 

  - (void)socket:(GCDAsyncSocket *)socket didAcceptNewSocket:(GCDAsyncSocket *)newSocket { NSLog(@"Accepted New Socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]); NSString *alertmsg = [NSString stringWithFormat:@"Client is now connected to %@.", self.service.name]; UIAlertView *Connection = [[UIAlertView alloc] initWithTitle:@"Connection Success!" message:alertmsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [Connection show]; [self setSocket:newSocket]; [connectedSockets addObject:newSocket]; // Read Data from Socket [newSocket readDataToLength:sizeof(uint64_t) withTimeout:-1.0 tag:0]; } - (void)socket:(GCDAsyncSocket *)socket didReadData:(NSData *)data withTag:(long)tag { if (tag == 0) { uint64_t bodyLength = [self parseHeader:data]; [socket readDataToLength:bodyLength withTimeout:-1.0 tag:1]; } else if (tag == 1) { [self parseBody:data]; [socket readDataToLength:sizeof(uint64_t) withTimeout:-1.0 tag:0]; } } 

Bien, lo descubrí! Espero que esto ayude a alguien en el futuro:

 - (void)startBroadcast { //socketQueue = dispatch_queue_create("socketQueue", NULL); self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; // Setup an array to store all accepted client connections connectedSockets = [[NSMutableArray alloc] initWithCapacity:20]; // Start Listening for Incoming Connections NSError *error = nil; if ([self.socket acceptOnPort:0 error:&error]) { // Initialize Service self.service = [[NSNetService alloc] initWithDomain:@"local." type:@"_iQuest._tcp." name:@"" port:[self.socket localPort]]; // Configure Service [self.service setDelegate:self]; // Publish Service [self.service publish]; } else { NSLog(@"Unable to create socket. Error %@ with user info %@.", error, [error userInfo]); } } 

 - (void)socket:(GCDAsyncSocket *)socket didAcceptNewSocket:(GCDAsyncSocket *)newSocket { NSLog(@"Accepted New Socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]); NSString *alertmsg = [NSString stringWithFormat:@"Client is now connected to %@.", self.service.name]; UIAlertView *Connection = [[UIAlertView alloc] initWithTitle:@"Connection Success!" message:alertmsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [Connection show]; @synchronized(connectedSockets) { dispatch_async(dispatch_get_main_queue(), ^{ [connectedSockets addObject:newSocket]; }); } // Read Data from Socket [newSocket readDataToLength:sizeof(uint64_t) withTimeout:-1.0 tag:0]; }