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;
NSU开发者_高级运维Integer 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 ,
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.
}
精彩评论