繁体   English   中英

使用NSInputStream时遇到问题

[英]Trouble using NSInputStream

尝试编写基本iPhone应用程序的总新手。 我为服务器编写了代码,可以将行从我的应用程序发送到服务器,但是在从服务器接收行时遇到了麻烦。

另外,这个程序似乎仅在我使用调试器运行它时才起作用。

任何帮助表示赞赏。 谢谢。

这是我的代码:

    - (void)viewDidLoad
 {
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     manager = [[CLLocationManager alloc] init];
     manager.distanceFilter = kCLDistanceFilterNone;
     manager.desiredAccuracy = kCLLocationAccuracyBest;
     [manager startUpdatingLocation];
     CLLocation *curLocation = [manager location];
     NSString *theLocation = curLocation.description;
     NSLog(@"%@", theLocation);

     //open the connection
     [self initNetworkConnection];
     NSData *data = [[NSData alloc] initWithData:[theLocation dataUsingEncoding:NSASCIIStringEncoding]];
      NSLog(@"%@", data);
     [outputStream write:[data bytes] maxLength:[data length]];

     //recieve data from the server
     locations = [[NSMutableArray alloc] init];
     NSLog(@"%@", locations);

     [inputStream close];
     [outputStream close];

 }

 -(void)initNetworkConnection {
     CFReadStreamRef readStream;
     CFWriteStreamRef writeStream;
     CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"137.165.8.168", 13413, &readStream, &writeStream);
NSLog(@"connection created!");
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;

//set delegates
[inputStream setDelegate:self];
[outputStream setDelegate:self];

//have processes perform in a run loop
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

//now open the streams
[inputStream open];
[outputStream open];

 }

 - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
     NSLog(@"got an event");
     switch (eventCode) {
         case NSStreamEventHasSpaceAvailable:
             NSLog(@"None!");
             break;
         case NSStreamEventOpenCompleted:
             NSLog(@"Stream opened");
             break;
         case NSStreamEventHasBytesAvailable:
             if (aStream == inputStream) {
                 uint8_t buffer[1024];
                 int len;
                 while ([inputStream hasBytesAvailable]) {
                     len = [inputStream read:buffer maxLength:sizeof(buffer)];
                     if (len > 0) {
                         NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                         if (nil != output) {
                             NSLog(@"%@",output);
                             [locations addObject:output];
                         }
                     }
                 }
             }

     }
 }

 - (void)didReceiveMemoryWarning
 {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
 }


 @end

您应调用-initNetworkConnection 之前实例化locations (并因此打开流)。 否则,当您收到第一个NSStreamEventHasBytesAvailable通知时, locations可能nil

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM