Question

J'ai une application iPhone qui est un client pour une application serveur existante.

J'utilise le code suivant pour me connecter et ce code fonctionnait bien. Aujourd'hui, j'ai commencé à travailler sur le projet et il se passe quelque chose d'étrange.

Lorsque je clique sur "Connexion" NSHost prend environ 30 à 45 secondes pour résoudre et se connecter à l'hôte. Une fois la connexion établie, les vitesses de transfert des données sont normales et rapides.

Le logiciel client d'origine (application PC) est connecté au même serveur et la connexion est géré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];
            self.navigationItem.prompt=@"Connection Error! Please check connection settings.";
        }

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

// Je reçois aussi maintenant ces avertissements. ** .. avec?! :)

  • avertissement: aucune méthode '+ hostWithAddress:' n'a été trouvée
  • warning: (Messages sans signature de méthode correspondante
  • avertissement: aucune méthode '+ hostWithName:' n'a été trouvée
  • avertissement: 'NSStream' peut ne pas répondre à '+ getStreamsToHost: port: inputStream: outputStream:'
Était-ce utile?

La solution

J'ai mis à jour mon code comme suit, ce 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);
}

Autres conseils

voici une autre façon de mettre en réseau en utilisant cfsockets

http://randomsnippetsofusefulstuff.blogspot.com/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top