How do i know if my client iOS app is connected to

2020-07-27 18:24发布

I press the button to connect to the server (TCP), but i don't know whether it connected or not.. Here is that part of the code:

[self connectToServerUsingCFStream:msg portNo:50000];

    if(readStream && writeStream)
    {
        NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; 
        statusText.text = newText;
        [newText release];
        pingButton.hidden = NO;
    }
    else
    {
        NSString *newText = [[NSString alloc] initWithFormat:@"Connection unsuccessful :("]; 
        statusText.text = newText;
        [newText release];
    }

I always get the "Connected!! :)" even if the server is offline :s

2条回答
Juvenile、少年°
2楼-- · 2020-07-27 18:56

Although you did not provide enough informations, I'd suggest to use ASIHTTPRequest for HTTP, and AsyncSocket for TCP and UDP. If an connection was established, callback methods will be triggered,

I have to say, that my experiences with CFNetwork are very limited, but for me it seems, as if you are just testing, if stream objects exists (if(readStream && writeStream)).
A quick look at CFNetwork Programming Guide: Working with Read Streams tells me, that you have to open it with CFReadStreamOpen(), this function will return an boolean, if it really did open the stream.

if (!CFReadStreamOpen(myReadStream)) {
    CFStreamError myErr = CFReadStreamGetError(myReadStream);
    // An error has occurred.
        if (myErr.domain == kCFStreamErrorDomainPOSIX) {
        // Interpret myErr.error as a UNIX errno.
        } else if (myErr.domain == kCFStreamErrorDomainMacOSStatus) {
        // Interpret myErr.error as a MacOS error code.
            OSStatus macError = (OSStatus)myErr.error;
        // Check other error domains.
    }
}

BTW:
instead of

NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; 
statusText.text = newText;
[newText release];

you just can write statusText.text = @"Connected!! :)";

查看更多
做个烂人
3楼-- · 2020-07-27 19:00

The solution for people following the connection method:

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];         
} 

is using the

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent

like this:

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSString *io;
if (theStream == iStream) io = @">>";
else io = @"<<";

NSString *event;
switch (streamEvent)
{
    case NSStreamEventNone:
        event = @"NSStreamEventNone";
        statusText.text =  @"Can not connect to the host!";
        break;

    case NSStreamEventOpenCompleted:
        event = @"NSStreamEventOpenCompleted";
        pingButton.hidden = NO;
        statusText.text = @"Connected";
        break;

    case NSStreamEventHasBytesAvailable:
        event = @"NSStreamEventHasBytesAvailable";
        if (theStream == iStream)
        {
            //read data
            uint8_t buffer[1024];
            int len;
            while ([iStream hasBytesAvailable])
            {
                len = [iStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0)
                {
                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                    NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
                    if (nil != output)
                    {
                        //do something with data
                    }
                }
            }
        }
        break;

    case NSStreamEventHasSpaceAvailable:
        event = @"NSStreamEventHasSpaceAvailable";
        if (theStream == oStream)
        {
            //send data
            uint8_t buffer[11] = "I send this";             
            int len;

            len = [oStream write:buffer maxLength:sizeof(buffer)];
            if (len > 0)
            {
                NSLog(@"Command send");
                [oStream close];
            }
        }

        break;

    case NSStreamEventErrorOccurred:
        event = @"NSStreamEventErrorOccurred";
        statusText.text = @"Can not connect to the host!";
        pingButton.hidden = YES;
        break;

    case NSStreamEventEndEncountered:
        event = @"NSStreamEventEndEncountered";
        statusText.text = @"Connection closed by the server.";
        pingButton.hidden = YES;
        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [theStream release];
        theStream = nil;
        break;

    default:
        event = @"** Unknown";
}

NSLog(@"%@ : %@", io, event);
}

(for all I know!) The credits go to deksa from this post (though i don't know who was the creator, because i've seen this some times on the web, including here o SO). This code was slightly modified by me (pingButton, statusText), if you want the original one go to the link previously mentioned.

The Apple Developer Site has some info on this as well.

Like i've said, I had seen some stuff looking like this on the web, but now i understand that everything that happens after you connect is "automatic"; for instance, if the server is on hold with a read(), the case NSStreamEventHasSpaceAvailable: will be called automatically, and all the code in there will be run.

Now I consider this question answered.

查看更多
登录 后发表回答