【问题标题】:GCDAsyncSocket Multiple Connections?GCDAsyncSocket 多个连接?
【发布时间】:2014-08-13 22:45:52
【问题描述】:

我已经在使用 GCDAsyncSocket 连接 2 个设备。一个广播自己并接受连接,另一个监听并请求连接。 如果我尝试将另一个设备连接到仍在广播的主机,它会在第一个设备仍然连接时连接然后终止连接!

如何重组我的代码以接受多个连接?我错过了什么?请帮忙,我正在努力解决这个问题!这是我的代码:

- (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];
        }
    }

【问题讨论】:

  • 您能否显示设置监听套接字的代码 - 即对 acceptOnPort 的调用?
  • 另外,对[self setSocket:newSocket] 的调用看起来很可疑。这个方法有什么作用?它是否正确处理多个连接的套接字?
  • 是的,好的,我已经添加了广播和监听连接的位置。我还怀疑“[self setSocket:newSocket]”,因为这只是添加了新连接。我在其他代码示例中看到过使用它:“[connectedSockets addObject:newSocket]”只是添加到一个数组中,但我不清楚如何修改代码!

标签: ios tcp wifi gcdasyncsocket


【解决方案1】:

好的,我想通了!我希望这对将来的某人有所帮助:

- (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];
}

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2013-12-22
    相关资源
    最近更新 更多