【问题标题】:NSHost taking an awfully long timeNSHost 花费了很长时间
【发布时间】:2009-05-04 14:20:48
【问题描述】:

我有一个 iPhone 应用程序,它是现有服务器应用程序的客户端。

我正在使用以下代码进行连接,并且此代码运行良好。今天我开始着手这个项目,但发生了一些奇怪的事情。

当我单击“连接”时,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];
            self.navigationItem.prompt=@"Connection Error! Please check connection settings.";
        }

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

//我现在也收到这些警告。**.. wth?! :)

  • 警告:找不到“+hostWithAddress:”方法
  • 警告:(没有匹配方法签名的消息
  • 警告:找不到“+hostWithName:”方法
  • 警告:“NSStream”可能无法响应“+getStreamsToHost:port:inputStream:outputStream:”

【问题讨论】:

  • iPhone 上既不存在 NSHost 也不存在 getStreamstoHost...

标签: iphone cocoa-touch sockets nshost


【解决方案1】:

我已将我的代码更新为以下内容,这解决了我的问题。

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

【讨论】:

    【解决方案2】:

    这是另一种使用 cfsockets 联网的方式

    http://randomsnippetsofusefulstuff.blogspot.com/

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 1970-01-01
      • 2017-05-10
      • 2015-08-02
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多