Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

Socket connection stream opened called two times

I have implemented socket connection in my project and it is working fine. But my problem is that I want to send a string request on opening completion of socket.

Delegate is working fine.

- (void)initNetworkCommunication
{
    lastString=@"";
    isSocketAuthorized=NO;
    NSLog(@"$$$$$$$$$$$$$$$$$$$$$$$$$============socket connection INITIALISED....");

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL,(CFStringRef)@"XXX.XX.XXX.XX",XXXX, &readStream, &writeStream);
    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];
}


- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
    switch (eventCode) {

        case NSStreamEventOpenCompleted:{

            NSLog(@"Stream opened");
            [self performSelectorInBackground:@selector(sendAuthRequest) withObject:nil];
        }
            break;

        case NSStreamEventHasBytesAvailable:
        {
            if (aStream == inputStream) {

                uint8_t buffer[1024];
                int len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        if (nil != output) {
//                            NSLog(@"server said: %@", output);
                            [self messageReceived:output];
                        }
                    }
                }
            }
        }
            break;

        case NSStreamEventErrorOccurred:
            NSLog(@"Can not connect to the host!");
            break;

        case NSStreamEventEndEncountered:
        {
            [aStream close];
            [aStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        }
            break;

        default:
            NSLog(@"Unknown event");
    }
}

But stream open is called twice.Why so? can anyone help me. Thanks.

Upvotes: 0

Views: 937

Answers (1)

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

After a long time of spending my brain over the problem , I come to know that I am actually calling open stream two times.First time for Input Stream and second time for Output Stream.And at last I got the solution to fire my method once in a no of event called,I used condition for NSStream class kind.

case NSStreamEventOpenCompleted:{

        if ([aStream isKindOfClass:[inputStream class]]) {
            NSLog(@"Input stream opened");
        }else{
            NSLog(@"output stream opened");
            [self performSelectorInBackground:@selector(sendAuthRequest) withObject:nil];
        }
    }

Upvotes: 5

Related Questions