Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用NSInputStream时出现问题_Ios_Objective C_Nsinputstream - Fatal编程技术网

Ios 使用NSInputStream时出现问题

Ios 使用NSInputStream时出现问题,ios,objective-c,nsinputstream,Ios,Objective C,Nsinputstream,尝试编写基本iPhone应用程序的新手。我为服务器编写了代码,可以从我的应用程序向服务器发送线路,但从服务器接收线路时遇到问题 此外,这个应用程序似乎只有在我使用调试器运行它时才能工作 感谢您的帮助。谢谢 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. man

尝试编写基本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并打开流之前,应该实例化位置。否则,当您收到第一个NSSTREAVENTHASBytesAvailable通知时,很可能位置为零。

可能不是您的问题,但您应该在另一个线程上执行此操作。