Archived Forum Post

Index of archived forum posts

Question:

Multiple sockets on the same "connection"

Mar 06 '13 at 07:59

Using ChilKat sockets we have an app that transfer data over a socket connection. Most pieces will be split into chunks so the socket is sending/receiving a set number of bytes. ie

[_socket SendBytes:dataChunk numBytes:[NSNumber numberWithInteger:chunkLength]]

And

NSData *data = [_socket ReceiveBytesN:[NSNumber numberWithInteger:chunkSize]];

As this could be a long running operation we have found that we also need to pass messages between instances of the application while the data transfer is ongoing. So what would make sense would be to set up an async socket on all instances that are both listening for messages but are also able to send messages. The thinking was something like this running on both server instance and client instance:

    - (void) startComms {

    while (YES) {
        if ([_commsSocket AsyncReceiveUntilMatch:@"-EOM-"]) {
            while (![_commsSocket AsyncReceiveFinished]) {
                [_commsSocket SleepMs:[NSNumber numberWithInt:100]];
            }
            if ([_commsSocket AsyncReceiveSuccess]) {
                NSLog(@"Received string: %@", _commsSocket.AsyncReceivedString);
            }
            else {
                NSLog(@"Comms failed");
            }
        }
        else {
            NSLog(@"failde to receive until match\n%@", _commsSocket.LastErrorText);
        }
    }
}

- (void) sendComm:(NSString *) message {

    if (![_commsSocket AsyncSendString:[message stringByAppendingString:@"-EOM-"]]) {
        NSLog(@"send err %@", _commsSocket.LastErrorText);
        return;
    }

    while (!_commsSocket.AsyncSendFinished) {
        [_commsSocket SleepMs:[NSNumber numberWithInt: 100]];
    }

    if (!_commsSocket.AsyncSendSuccess != YES) {
        NSLog(@"send success err %@", _commsSocket.AsyncSendLog);
    }
}

Is this the way to go about it? How can we establish this second socket connection between the connected pair?