2014-01-21 1 views
1

기본 iPhone 응용 프로그램을 프로그래밍하려고하는 초보자입니다. 내가 서버 코드를 작성하고 내 애플 리케이션에서 서버로 라인을 보낼 수 있지만 내 서버에서 라인을받는 데 문제가있다.NSInputStream을 사용하는 데 문제가 있습니다.

또한이 응용 프로그램은 디버거를 사용하여 실행할 때만 작동하는 것으로 보입니다.

도움을 주시면 감사하겠습니다. 감사.

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

아마도 문제는 아니지만 다른 스레드에서이를 수행해야합니다. – Kevin

답변

0

당신은 -initNetworkConnection 전화 (그래서 당신의 스트림을 엽니) 이전 locations를 인스턴스화해야합니다

여기 내 코드입니다. 그렇지 않으면 처음 NSStreamEventHasBytesAvailable 알림을받을 때까지 locationsnil입니다.