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

我使用了下列代码连接,而这个代码工作的罚款。今天我开始该项目的工作,并奇怪的东西正在发生的事情。

当我点击"连接"NSHost需要大约30-45秒解决和连接对主机。一旦建立了连接、数据传输速度是正常和快速。

我已经原有客户机软件(电脑应用程序)连接到相同的服务器,以及连接是立即处理!!

也许有人可以揭示一些主题...

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

//I am现在也得到这些警告。**..问心无愧?!:)

  • 警告:没有'+hostWithAddress:'的方法找到的
  • 警告:(息没有匹配的方法的签名
  • 警告:没有'+hostWithName:'的方法找到的
  • 警告:'NSStream'可以不回应'+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);
}

其他提示

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

http://randomsnippetsofusefulstuff.blogspot.com/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top