NSHost impiega moltissimo tempo
-
03-07-2019 - |
Domanda
Ho un'app per iPhone che è un client per un'applicazione server esistente.
Sto usando il seguente codice per connettermi e questo codice funzionava bene. Oggi ho iniziato a lavorare al progetto e sta accadendo qualcosa di strano.
Quando faccio clic su " Connetti " NSHost impiega circa 30-45 secondi per risolvere e connettersi all'host. Una volta stabilita la connessione, le velocità di trasferimento dei dati sono normali e veloci.
Ho il software client originale (Applicazione PC) che si collega allo stesso server e la connessione viene gestita immediatamente !!
Forse qualcuno può far luce sull'argomento ...
-(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.";
}
}
// Adesso sto anche ricevendo questi avvisi. ** .. wth ?! :)
- avviso: nessun metodo '+ hostWithAddress:' trovato
- avvertenza: (Messaggi senza firma del metodo corrispondente
- avviso: nessun metodo '+ hostWithName:' trovato
- avviso: "NSStream" potrebbe non rispondere a "+ getStreamsToHost: port: inputStream: outputStream:"
Soluzione
Ho aggiornato il mio codice al seguente, che ha risolto il mio 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);
}
Altri suggerimenti
ecco un altro modo di fare rete usando cfsocket