我一直在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] Ditected URL为http://192.168.4.152/test.html
2013-07-09 16:49:47.612 SocketProgram [4055:c07] stream:handleEvent:被调用...
2013-07-09 16:49:47.612 SocketProgram [4055:c07]事件------ NSStreamEventOpenCompleted
2013-07-09 16:49:47.613 SocketProgram [4055:c07] stream:handleEvent:被调用...
2013-07-09 16:49:47.613 SocketProgram [4055:c07]事件------ NSStreamEventOpenCompleted
2013-07-09 16:49:47.613 SocketProgram [4055:c07] stream:handleEvent:被调用...
2013-07-09 16:49:47.614 SocketProgram [4055:c07]事件------ NSStreamEventHasSpaceAvailable
2013-07-09 16:49:47.617 SocketProgram [4055:c07] stream:handleEvent:被调用...
2013-07-09 16:49:47.617 SocketProgram [4055:c07]事件------ NSStreamEventHasBytesAvailable
2013-07-09 16:49:47.617 SocketProgram [4055:c07]长度是-------------------- 955
2013-07-09 16:49:47.618 SocketProgram [4055:c07]字节读取:(null)
2013-07-09 16:49:47.618 SocketProgram [4055:c07] stream: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