Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6023933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:04:48+00:00 2026-05-23T04:04:48+00:00

I press the button to connect to the server (TCP), but i don’t know

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:04:49+00:00Added an answer on May 23, 2026 at 4:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, when i press the connect button i will be connected to the
I'm trying to fill-out a form automatically and press a button on that form
I am not connected to my server and i type some text in the
Previous Problem (I am not connected to my server and i type some text
After I press a button, Android parse a JSON file and pick the info
How do I detect a button press on a USB gamepad on OSX 10.5
This is when i press Like button. How to inspect where is the problem?
I am wanting to press a button and alternate between two images with one
I have a form where you may press a button which will give you
How can I flip an NSWindow so I can press a button, and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.