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 6052257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:54:31+00:00 2026-05-23T07:54:31+00:00

I am using tcp connection to connect a server using ip and port. I

  • 0

I am using tcp connection to connect a server using ip and port. I can write and read stream on it. my problem is when I turn off server .app stop with “EXC_BAD_ACCESS” can anyone help me?

this is connect code:

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

    if (![urlStr isEqualToString:@""]) 
    {
        NSURL *website = [NSURL URLWithString:urlStr];
        if (!website) 
        {
            NSLog(@"%@ is not a valid URL");
            return;
        }
        else
        {
            [NSStream getStreamsToHostNamed:urlStr 
                                       port:portNo 
                                inputStream:&iStream
                               outputStream:&oStream];            
            [iStream retain];
            [oStream retain];

            [iStream setDelegate:self];
            [oStream setDelegate:self];

            [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];
            [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];
            [oStream open];

            [iStream open];            
        }
    }    
}

and this is stream event delegate:

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
    switch(eventCode) {
        case NSStreamEventHasBytesAvailable:
        {
            if (data == nil) 
            {
                data = [[NSMutableData alloc] init];
            }
            uint8_t buf[1024];
            unsigned int len = 0;
            len = [(NSInputStream *)stream read:buf maxLength:1024];
            if(len) 
            {    
                [data appendBytes:(const void *)buf length:len];
                int bytesRead;
                bytesRead += len;

            }
            else 
            {

                NSLog(@"No data.");
                return;
            }

            NSString *str = [[NSString alloc] initWithData:data 
                                                  encoding:NSUTF8StringEncoding];
            NSLog(@"From server: %@",str);
            [str release];
            [data release];        
            data = nil;
            break;
        } 
        case NSStreamEventErrorOccurred:
        {
            NSError *theError = [stream streamError];
            NSLog(@"Error reading stream! ,Error %i: %@",[theError code], [theError localizedDescription]);
            [self disconnect];
            [self connectToServerUsingStream:kHostIP portNo:kPort];
            break;
        }
        case NSStreamEventHasSpaceAvailable:
        {
            if(stream == oStream && !isDataSent)
            {
                isDataSent = YES;
                [self writeToServer:@"HI"];
            }
            break;
        }
    }
}
  • 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-23T07:54:31+00:00Added an answer on May 23, 2026 at 7:54 am

    What logging messages are you getting so you can see which parts of your code get called?

    Have you tried setting some breakpoints and then stepping through to see the exact point of failure and also then inspect the objects in case you aren’t retaining/releasing properly?

    What other debugging steps have you taken so far?

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

Sidebar

Related Questions

I have a web app which connects to a server using a TCP connection
I have a weird problem. I can connect to a remote host using psql
I am trying to tcp connect to a server machine in java, by using
I implemented TCP client to connect to the server using TcpClient ( C# .NET
I have a server that connects to multiple clients using TCP/IP connections, using C
I am using the BLIP/MYNetwork library to establish a basic tcp socket connection between
I'm using this kind of code for my TCP/IP connection: sock = new Socket(host,
I am using creating a server/client style program, and it uses tcp connections. Now,
I'm using TCP/IP over ActiveSync to connect from Windows CE device to Windows XP
I built a TicTacToe game application (I'm using TCP protocol) that consist of server

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.