سؤال

لدي تطبيق 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.";
    }
}

// أنا الآن أتلقى هذه التحذيرات أيضًا.**..مع؟!:)

  • تحذير:لم يتم العثور على طريقة '+hostWithAddress:'
  • تحذير:(الرسائل بدون توقيع طريقة مطابقة
  • تحذير:لم يتم العثور على طريقة '+hostWithName:'
  • تحذير:قد لا يستجيب "NSStream" لـ "+getStreamsToHost:port:inputStream:outputStream:"
هل كانت مفيدة؟

المحلول

لقد قمت بتحديث الكود الخاص بي إلى ما يلي، مما أدى إلى حل مشكلتي.

-(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