Pergunta

Eu tenho um iPhone app que é um cliente para um aplicativo de servidor existente.

Eu estou usando o seguinte código para se conectar, e este código estava funcionando bem. Hoje eu comecei a trabalhar no projeto, e algo estranho está acontecendo.

Quando eu clique em "Connect" NSHost leva cerca de 30-45 segundos para resolver e se conectar ao host. Uma vez que a conexão é estabelecida, as velocidades de transferência de dados são normais e rápido.

Eu tenho o software cliente original (PC Aplicação) ligando para o mesmo servidor, ea conexão é tratado imediatamente !!

Talvez alguém pode lançar alguma luz sobre o assunto ...

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

// Eu também estou começando agora esses avisos. ** .. wth ?! :)

  • aviso: não '+ hostWithAddress:' método encontrado
  • aviso: (Mensagens sem a assinatura do método de harmonização
  • aviso: não '+ hostWithName:' método encontrado
  • aviso: 'NSStream' pode não responder a '+ getStreamsToHost: port: inputStream: outputStream:'
Foi útil?

Solução

Eu atualizei meu código para o seguinte, que resolveu o meu problema.

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

Outras dicas

aqui é uma outra maneira de rede usando cfsockets

http://randomsnippetsofusefulstuff.blogspot.com/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top