0
在我的應用程序,已經設置了流這樣,可可Socket編程NSInputStream讀返回0
(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];
}
,並像下面來處理事件,
- (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];
}
}
}
}
問題是,閱讀或的GetBuffer總回報0,我錯過了什麼?
由於事先
謝謝,我試過了,但是沒有任何改善,它總是返回0並立即關閉 – Amitg2k12
您是否使用Wireshark進行網絡跟蹤?該流實際上是否包含數據,還是有可能該流真的被對等關閉了? – DarkDust
謝謝,現在這樣做,剛剛經歷了一些更多的博客,似乎服務器可能關閉了套接字,因此流中沒有任何內容......但是曲目將非常有用......。 – Amitg2k12