質問

既存のサーバーアプリケーションのクライアントであるiPhoneアプリがあります。

次のコードを使用して接続していますが、このコードは正常に機能していました。今日、私はプロジェクトの作業を開始しましたが、奇妙なことが起こっています。

[接続]をクリックすると、 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