Cocoa 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];
}
}
}
}
问题是,read 或 getBuffer 总是返回 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 ,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您的情况下
getBuffer:lenght:
的问题是什么,但这就是应该使用read:maxLength:
的方式:Not sure what the problem is with
getBuffer:lenght:
in your case, but this is howread:maxLength:
should be used: