NSHost tomando un tiempo muy largo
-
03-07-2019 - |
Pregunta
Tengo una aplicación para iPhone que es un cliente para una aplicación de servidor existente.
Estoy usando el siguiente código para conectarme, y este código funcionaba bien. Hoy comencé a trabajar en el proyecto, y algo extraño está sucediendo.
Cuando hago clic en " Conectar " NSHost tarda unos 30-45 segundos en resolverse y conectarse al host. Una vez que se establece la conexión, las velocidades de transferencia de datos son normales y rápidas.
¡Tengo el software cliente original (aplicación de PC) conectado al mismo servidor, y la conexión se maneja de inmediato!
Quizás alguien pueda arrojar algo de luz sobre el tema ...
-(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.";
}
}
// Ahora también estoy recibiendo estas advertencias. ** .. wth ?! :)
- advertencia: no '+ hostWithAddress:' método encontrado
- advertencia: (Mensajes sin una firma de método coincidente
- advertencia: no '+ hostWithName:' método encontrado
- advertencia: 'NSStream' puede no responder a '+ getStreamsToHost: puerto: inputStream: outputStream:'
Solución
He actualizado mi código a la siguiente, que ha resuelto mi 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);
}
Otros consejos
Esta es otra forma de conexión en red mediante cfsockets