简体   繁体   中英

Wait before returning value iPhone

I found some classes on the internet to establish a tcp connection. The link is in here . I want to customize a class and so far I am able to establish a connection send data and receive data which is great. Once I have created and imported the classes used by the first link that I provided I am able to establish a connection using the following method:

-(void) connectToServerUsingCFStream:(NSString *) urlStr portNo: (uint) portNo {

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, 
                                       (CFStringRef) urlStr, 
                                       portNo, 
                                       &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];    

    }
}

Since I am going to use this class a lot on my application I am creating my own class and I want to create a -(BOOL) connect{} method. I want to return yes is the connection is establish and no otherwise. The problem is that the way I am able to tell if I establish a connection is by creating a connection attempting to send data and then on the server side I have created a method that whenever I receive that I send a string back. If in the next 2 seconds I receive data I know that the connection was establish.

so in my connection method I want to wait 2 seconds and then return a value depending if the BOOL variable didReciveData = YES.

Since you use a NSOutputStream a better approach could be to check the return value of [oStream write:] : if it returns -1 no data has been sent, so there's no connection.

However, if you want to wait two seconds you can use NSTimer to create a timeout. If you receive a response before the timer fires you can invalidate the timer, otherwise the timer will call the related method that will notify the end of the two seconds.

You could try pausing the main run loop.

- (void)test
{
    NSLog(@"Test starting.");

    BOOL wasSuccessful = [self connect];
    NSLog(@"Success: %d", wasSuccessful);
}

- (BOOL)connect
{
    // try to connect here, make sure to get a callback on success/failure

    // fake callback
    [self performSelector:@selector(callback:) withObject:[NSNumber numberWithBool:NO] afterDelay:2.0];

    // wait for callback
    CFRunLoopRun();
    return self.success;
}

- (void)callback:(NSNumber *)successful
{
    self.success = [successful boolValue];

    CFRunLoopStop(CFRunLoopGetCurrent()); // now we want -connect to return!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM