1

既存のサーバー アプリケーションのクライアントである 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:' に応答しない可能性があります
4

2 に答える 2

1

コードを次のように更新したことで、問題が解決しました。

-(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);
}
于 2009-05-04T15:00:12.140 に答える
0

cfsockets を使用したネットワークの別の方法を次に示します。

http://randomsnippetsofusefulstuff.blogspot.com/

于 2009-08-26T15:19:41.730 に答える