0

In my App, have setup the stream like this,

(void)connectStream:(NSString *)pHostName PortNo:(int)inPortNo HasInput:(bool)bInput HasOutput:(bool)bOutput{




    NSHost *host = [NSHost hostWithName:pHostName];

    //host = [NSHost hostWithAddress:pHostName];

    [NSStream getStreamsToHost:host port:inPortNo inputStream:&pInputStream
                  outputStream:&pOutputStream];

    [pInputStream retain];  

    [pOutputStream retain];

    [pInputStream setDelegate:self];

    [pOutputStream setDelegate:self];



    bool bUseSSL = YES;
    if (bUseSSL)
    { 

        [self setInputStreamSecurity];
        [self setOutputStreamSecurity];
    }


    [pOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                       forMode:NSDefaultRunLoopMode];

    [pInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                            forMode:NSDefaultRunLoopMode];


    [pInputStream open];

    [pOutputStream open];

}

and event handled like below,

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent{

       switch(streamEvent){
        case NSStreamEventHasBytesAvailable:{
        if([theStream hasBytesAvailable]){
                unsigned int len=0;

                NSUInteger intLen;
                [theStream getBuffer:&pInputBuffer length:&intLen];
                [theStream read:pInputBuffer maxLength:MAX_INPUT_BUFF_LEN];

                if(intLen){         
                  NSMutableData *data=[[NSMutableData alloc] init];
                  [data appendBytes:pInputBuffer length:len];

                  [WebSocketEventData postGotBytesEvent:data Len:len];
                 }else{
                   NSError *theError = [theStream streamError];
                   NSString *pString = [theError localizedDescription];
                   int errorCode = [theError code];


                }

              }
    }
}

The problem is, read or getBuffer always returns 0, Am i missing something?

Thanks in Advance ,

George Johnston
  • 31,652
  • 27
  • 127
  • 172
Amitg2k12
  • 3,765
  • 10
  • 48
  • 97

1 Answers1

0

Not sure what the problem is with getBuffer:lenght: in your case, but this is how read:maxLength: should be used:

NSInteger bytesRead;

bytesRead = [theStream read:pInputBuffer maxLength:MAX_INPUT_BUFF_LEN];
if (bytesRead > 0) {
    // Handle input.
} else if (bytesRead == 0) {
    // Handle EOF.
} else {
    // Handle error.
}
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • Thanks, i tried that too, but no improvement, it always returns 0 and close immediately – Amitg2k12 Aug 11 '11 at 07:38
  • Did you do a network trace with Wireshark ? Does the stream actually contain data or is it possible that the stream really is closed by the peer ? – DarkDust Aug 11 '11 at 07:45
  • Thanks , doing that now, just gone through some more blogs, it seems server might have closed socket so there is nothing in the stream... but track will really helpful ... . – Amitg2k12 Aug 11 '11 at 09:17