2009-05-04 306 views
1

我有一個iPhone應用程序,它是現有服務器應用程序的客戶端。NSHost需要很長時間

我正在使用下面的代碼連接,並且此代碼工作正常。今天我開始研究這個項目,發生了一些奇怪的事情。

當我點擊「連接」時,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]; 
      [email protected]"Connection Error! Please check connection settings."; 
     } 

    }else{ 
     [self resetConnectionAndScreen]; 
     [email protected]"Connection Error! Please check connection settings."; 
    } 
} 

//我現在還讓這些警告。** ..問心無愧? :)

  • 警告:無 '+ hostWithAddress:' 方法發現
  • 警告:(消息而沒有匹配的方法簽名
  • 警告:無 '+ hostWithName:' 方法發現
  • 警告:「NSStream端口:的inputStream:OutputStream中:+ getStreamsToHost'可不迴應 ''
+0

既不`NSHost`也不`getStreamstoHost ...`在iPhone上存在 – user102008 2011-01-06 05:23:02

回答

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); 
}