문제

기존 서버 응용 프로그램의 클라이언트 인 iPhone 앱이 있습니다.

다음 코드를 사용하여 연결하고 있으며이 코드는 제대로 작동했습니다. 오늘 저는 프로젝트 작업을 시작했고 이상한 일이 일어나고 있습니다.

"Connect"를 클릭하면 nshost가 호스트를 해결하고 연결하는 데 약 30-45 초가 걸립니다. 연결이 설정되면 데이터 전송 속도는 정상적이고 빠릅니다.

원래 클라이언트 소프트웨어 (PC 응용 프로그램)가 동일한 서버에 연결되어 있으며 연결이 즉시 처리됩니다 !!

아마도 누군가가 그 주제에 대해 약간의 빛을 발할 수있을 것입니다 ...

-(IBAction)tryConnection{
    [self showLogoffButtons];
    iPhone_PNPAppDelegate *mainDelegate = (iPhone_PNPAppDelegate *)[[UIApplication sharedApplication] delegate];
    settings=mainDelegate.settings; 
    [activityindicator startAnimating];
    sendState=0;

    NSHost* host;

    //30-45 second pause happens here (since I am using an IP Address)
    host = [NSHost hostWithAddress:settings.masterLocation];
    //NSHost returns (eventually) with the correct connection.

    if (!host)  {
        host = [NSHost hostWithName:settings.masterLocation];
    }
    if( host ) {
        [NSStream getStreamsToHost:host port:[settings.masterPort intValue] inputStream:&iStream outputStream:&oStream] ;
        if( nil != iStream && nil != oStream ) {
            [iStream retain];
            [oStream retain];
            [iStream setDelegate:self];
            [oStream setDelegate:self];
            [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [iStream open];
            [oStream open]; 
        }else{
            [self resetConnectionAndScreen];
            self.navigationItem.prompt=@"Connection Error! Please check connection settings.";
        }

    }else{
        [self resetConnectionAndScreen];
        self.navigationItem.prompt=@"Connection Error! Please check connection settings.";
    }
}

// 나는 또한이 경고를 받고 있습니다. ** .. wth?! :)

  • 경고 : 아니요 '+hostwithAddress :'메소드가 발견되었습니다.
  • 경고 : (일치하는 메소드 서명이없는 메시지
  • 경고 : 아니요 '+hostwithName :'메소드가 발견되었습니다.
  • 경고 : 'nsstream'은 '+getStreamstohost : port : inputStream : outputStream :'에 응답 할 수 없습니다.
도움이 되었습니까?

해결책

내 코드를 다음으로 업데이트했는데 문제가 해결되었습니다.

-(IBAction)tryConnection{   
    CFReadStreamRef readStream = NULL;
    CFWriteStreamRef writeStream = NULL;
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)settings.masterLocation, [settings.masterPort intValue], &readStream, &writeStream);
    if (readStream && writeStream) {
        CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
        CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

        iStream = (NSInputStream *)readStream;
        [iStream retain];
        [iStream setDelegate:self];
        [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [iStream open];

        oStream = (NSOutputStream *)writeStream;
        [oStream retain];
        [oStream setDelegate:self];
        [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [oStream open];     
    }
    if (readStream) CFRelease(readStream);
    if (writeStream) CFRelease(writeStream);
}

다른 팁

CFSockets를 사용하여 네트워킹의 또 다른 방법은 다음과 같습니다

http://randomsnippetsofusefulstuff.blogspot.com/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top