2009-05-04 7 views
1

J'ai une application iPhone qui est un client pour une application serveur existante. J'utilise le code suivant pour se connecter et ce code fonctionnait correctement. Aujourd'hui, j'ai commencé à travailler sur le projet, et quelque chose d'étrange se passe. Lorsque je clique sur "Connecter" NSHost prend environ 30-45 secondes pour résoudre et se connecter à l'hôte. Une fois la connexion établie, les vitesses de transfert de données sont normales et rapides.NSHost prend énormément de temps

J'ai le logiciel client original (Application PC) se connectant au même serveur, et la connexion est traitée immédiatement !!

Peut-être que quelqu'un peut faire la lumière sur le sujet ...

-(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."; 
    } 
} 

// Je suis aussi à avoir ces avertissements. ** .. wth ?! :)

  • avertissement: pas '+ hostWithAddress:' méthode trouvé avertissement
  • : (Messages sans signature de la méthode de mise en correspondance
  • avertissement: pas '+ hostWithName:' méthode trouvé
  • avertissement: « NSStream «ne peut pas répondre à '+ getStreamsToHost: port: fluxEntrée: outputStream:.'
+0

ni '' NSHost' ni getStreamstoHost ... 'existe sur l'iPhone – user102008

Répondre

1

J'ai mis à jour mon code à ce qui suit, qui a résolu mon problème

-(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); 
} 
Questions connexes