Multiple GCDAsyncSocket connections?
I am already using GCDAsyncSocket to connect 2 devices. One is broadcasting and receiving a connection, the other is listening and requesting a connection. If I try to connect another device to the host still transmitting, it connects and then terminates the connection while the first device is still connected!
How can I refactor my code to accept multiple connections? what am i missing? Please help, I am trying so hard to figure it out! Here's my code:
- (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];
}
}
+3
source to share
1 answer
OK, I figured it out! I hope this helps someone in the future:
- (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];
}
+4
source to share