我一直在 IOS 的流程序中工作,我需要建立与具有指定 IP 地址和端口号的机器的套接字连接。最终目标是检索响应流并显示在客户端机器上。我使用下面的代码,但它没有正确读取数据(字节)。NSStreamDelegate 的流处理程序到达其“NSStreamEventHasBytesAvailable”事件代码,但读取数据时出现问题。如果有人能解决这个问题,那将是一个很大的帮助
-(IBAction)connectToServer:(id)sender
{
NSString *portNo_=@"80";
NSString *serviceName_=@"web";
[self establishConnection:serviceName_ forIpAddress:@"`192.168.4.152/test.html`" forPort:portNo_];
}
- (void)establishConnection:(NSString*)serviceName forIpAddress:(NSString *)ipAddress forPort:(NSString *)portNo
{
NSLog(@"connection method called");
if(inputStream && outputStream)
{
[self close];
}
urlString = [NSString stringWithFormat:@"http://%@", ipAddress];
NSLog(@"Ditected URL is %@ ",urlString);
NSURL *website = [NSURL URLWithString:urlString];
if (!website)
{
NSLog(@"%@ is not a valid URL", website);
}
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], [portNo intValue], &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
inputStream = (NSInputStream *)readStream;
outputStream = (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
{
NSLog(@"stream:handleEvent: is invoked...");
NSError *theError;
NSMutableData *data=[[NSMutableData alloc]init];
NSString *event;
NSNumber *bytesRead=[[NSNumber alloc] init];
switch (eventCode)
{
case NSStreamEventNone:
event = @"NSStreamEventNone";
NSLog(@"event------%@",event);
break;
case NSStreamEventOpenCompleted:
event = @"NSStreamEventOpenCompleted";
NSLog(@"event------%@",event);
break;
case NSStreamEventHasSpaceAvailable:
event = @"NSStreamEventHasSpaceAvailable";
NSLog(@"event------%@",event);
if (aStream == outputStream)
{
NSString * str = [NSString stringWithFormat:@"GET / HTTP/1.0\r\n\r\n"];
const uint8_t *rawstring =(const uint8_t *)[str UTF8String];
[outputStream write:rawstring maxLength:strlen(rawstring)];
[outputStream close];
}
break;
case NSStreamEventHasBytesAvailable:
event = @"NSStreamEventHasBytesAvailable";
NSLog(@"event------%@",event);
if (aStream == inputStream)
{
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)aStream read:buf maxLength:1024];
if(!data)
{
data = [[NSMutableData data] retain];
}
@try
{
if(len)
{
NSLog(@"length is --------------------%d", len);
[data appendBytes:(const void *)buf length:len];
[bytesRead setIntValue:[bytesRead intValue]+len];
NSLog(@"Bytes Read : %@ ",bytesRead);
}
else
{
NSLog(@"no buffer!");
}
}
@catch (NSException *exception)
{
NSString *ex=[[NSString alloc]initWithFormat:@"Exception Occured:%@ . Reason:%@",[exception name],[exception reason]];
NSLog(@"%@",ex);
}
break;
}
case NSStreamEventErrorOccurred:
theError = [aStream streamError];
event = [[NSString alloc] initWithFormat:@"NSStreamEventErrorOccurred %@ ",theError];
NSLog(@"event------%@",event);
[self close];
break;
case NSStreamEventEndEncountered:
event = @"NSStreamEventEndEncountered";
NSLog(@"event------%@",event);
[aStream close];
[aStream removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[aStream release];
aStream=nil;
break;
default:
event=@"Unknown";
}
}
我的输出看起来像这样,
3-07-09 16:49:47.602 SocketProgram[4055:c07] 连接方法称为 2013-07-09 16:49:47.604 SocketProgram[4055:c07] 检测到的 URL 是http://192.168.4.152/test.html
618 SocketProgram[4055:c07] 流:handleEvent:被调用... 2013-07-09 16:49:47.618 SocketProgram[4055:c07] 事件------NSStreamEventHasBytesAvailable 2013-07-09 16:49:47.618 SocketProgram[4055:c07] 没有缓冲区!2013-07-09 16:49:47.619 SocketProgram[4055:c07] stream:handleEvent: 被调用... 2013-07-09 16:49:47.619 SocketProgram[4055:c07] 事件-----NSStreamEventEndEncountered