简体   繁体   中英

Trouble using NSInputStream

Total novice trying to program a basic iPhone app. I wrote the code for a server and can send lines from my app to the server but I'm having trouble receiving lines from my server.

Also this app seems to only work when I run through it using the debugger.

Any help is appreciated. Thanks.

Here's my code:

    - (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

You should instantiate locations before you call -initNetworkConnection (and so open your streams). Otherwise, it's likely locations is nil by the time you get your first NSStreamEventHasBytesAvailable notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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