NSHost需要很长时间
问题描述:
我有一个iPhone应用程序,它是现有服务器应用程序的客户端。NSHost需要很长时间
我正在使用下面的代码连接,并且此代码工作正常。今天我开始研究这个项目,发生了一些奇怪的事情。
当我点击“连接”时,NSHost需要大约30-45秒才能解析并连接到主机。连接建立后,数据传输速度正常且快速。
我有原来的客户端软件(PC应用程序)连接到同一台服务器,并立即处理连接!
也许有人可以提供一些线索关于这个问题...
-(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.";
}
}
//我现在还让这些警告。** ..问心无愧? :)
- 警告:无 '+ hostWithAddress:' 方法发现
- 警告:(消息而没有匹配的方法签名
- 警告:无 '+ hostWithName:' 方法发现
- 警告:“NSStream端口:的inputStream:OutputStream中:+ getStreamsToHost'可不回应 ''
答
我已经更新了我的代码以下,这解决了我的问题
-(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);
}
既不`NSHost`也不`getStreamstoHost ...`在iPhone上存在 – user102008 2011-01-06 05:23:02