1

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

4
  • What kind of server? HTTP? FTP? …? or something over TCP/IP? Jun 15, 2011 at 11:19
  • 3
    If your iPhone starts burning or explodes when you press the button, its most likely not connected. However, a not burning iPhone could also indicate that its underwater.
    – JustSid
    Jun 15, 2011 at 11:19
  • and provide code. I lost my Crytsal Ball. Jun 15, 2011 at 11:20
  • @JustSid ahah. @vikingosegundo deves perceber isto não? já postei mais info no outro comment..
    – StinkyCat
    Jun 15, 2011 at 11:44

2 Answers 2

7

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.

1

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!! :)";

13
  • oh right lol so the connection is TCP and i connected with [self connectToServerUsingCFStream:msg portNo:50000];. Those callback methods, that's what i want to know lol (how do i use the tag 'code' btw?)
    – StinkyCat
    Jun 15, 2011 at 11:41
  • ups! its actually CFStreamCreatePairWithSocketToHost that i use to connect to the server.
    – StinkyCat
    Jun 15, 2011 at 11:50
  • hum.. i looked around and maybe this help (i had a code similar to this written by someone else but didnt work..): If the socket connection fails, then one or both of the requested CFReadStreamRef and CFWriteStreamRef objects are NULL. I'm gonna try it out now.
    – StinkyCat
    Jun 15, 2011 at 12:01
  • I never succeeded in using the CFNetwork stuff. And I tried… AsyncSocket is a thin layer around CFNetwork*, that will give you simple to use classes and callback methods to make your life much easier. And when I say much, I mean MUUUCH. Jun 15, 2011 at 13:24
  • hum.. ok @vikingosegundo, i've been avoiding it, but im gonna check it out. I'll post some feedback on it. thks
    – StinkyCat
    Jun 15, 2011 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.