3

私はすでに GCDAsyncSocket を使用して 2 つのデバイスを接続しています。1 つは自分自身をブロードキャストして接続を受け入れ、もう 1 つはリッスンして接続を要求します。まだブロードキャストしているホストに別のデバイスを接続しようとすると、最初のデバイスがまだ接続されている間に接続が終了します。

複数の接続を受け入れるようにコードを再構築するにはどうすればよいですか? 私は何が欠けていますか?助けてください、私はこれを理解しようと一生懸命努力しています! これが私のコードです:

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

1 に答える 1

4

わかりました!これが将来誰かに役立つことを願っています:

- (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];
}
于 2014-08-15T03:02:58.973 に答える