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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:40:41+00:00 2026-06-05T02:40:41+00:00

I am trying to use the SmallSockets library to create a TCP socket connection.

  • 0

I am trying to use the SmallSockets library to create a TCP socket connection. Currently, I’m using a button to simply test my connection to a server from my iPhone. Here is what my code looks like:

-(IBAction)btnConnect:(id)sender
{
    bool loopConnection = true;
    while(loopConnection == true)
    {
        Socket *socket;
        int port = 11005;
        NSString *host = @"199.5.83.63";

        socket = [Socket socket];

        @try
        {
            NSMutableData *data;
            [socket connectToHostName:host port:port];
            [socket readData:data];
            //  [socket writeString:@"Hello World!"];

            //** Connection was successful **//
            [socket retain]; // Must retain if want to use out of this action block.
        }
        @catch (NSException* exception) 
        {
            NSString *errMsg = [NSString stringWithFormat:@"%@",[exception reason]];
            NSLog(errMsg);
            socket = nil;
        }
    }
}

When I press the button, my app freezes. It freezes on the following function (which is part of the SmallSockets library):

- (int)readData:(NSMutableData*)data
//
// Append any available data from the socket to the supplied buffer.
// Returns number of bytes received.  (May be 0)
//
{
    ssize_t count;

    // data must not be null ptr

    if ( data == NULL )
    [NSException raise:SOCKET_EX_INVALID_BUFFER 
                    format:SOCKET_EX_INVALID_BUFFER];

    // Socket must be created and connected

    if ( socketfd == SOCKET_INVALID_DESCRIPTOR )
        [NSException raise:SOCKET_EX_BAD_SOCKET_DESCRIPTOR 
                    format:SOCKET_EX_BAD_SOCKET_DESCRIPTOR];

    if ( !connected )
        [NSException raise:SOCKET_EX_NOT_CONNECTED 
                    format:SOCKET_EX_NOT_CONNECTED];

    // Request a read of as much as we can.  Should return immediately if no data.

    count = recv(socketfd, readBuffer, readBufferSize, 0);

    if ( count > 0 )
    {
        // Got some data, append it to user's buffer

        [data appendBytes:readBuffer length:count];
    }
    else if ( count == 0 )
    {
        // Other side has disconnected, so close down our socket

        [self close];
    }
    else if ( count < 0 )
    {
        // recv() returned an error. 

        if ( errno == EAGAIN )
        {
            // No data available to read (and socket is non-blocking)
            count = 0;
        }
        else
            [NSException raise:SOCKET_EX_RECV_FAILED 
                        format:SOCKET_EX_RECV_FAILED_F, strerror(errno)];
    }

    return count;
}

The app freezes on the line: [data appendBytes:readBuffer length:count]; and then it says on the same line: Thread 1:EXC_BAD_ACCESS (code=1, address=0x5d18c48b). All I see in the output is (lldb) in green letters. As soon as a client connects, the server sends a 4 byte packet to the client.

If anyone could shed some light as to why my app is crashing here, I’d really appreciate it. I’ve been banging my head for a couple hours now trying to figure out what the issue is.

Thanks!

  • 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-06-05T02:40:43+00:00Added an answer on June 5, 2026 at 2:40 am

    I don’t know if this is the right answer or not, but the first thing I would do is retain the socket right away. If you have a problem with the socket, then release it in the exception handler.

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

Sidebar

Related Questions

Anyone know how to create a cvCreateStructuringElementEx using a image? I'm trying use opencv.cv.cvCreateStructuringElementEx()
I was trying use svn to find a checkin using svn log, but it
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Trying to use code from here Im trying to incorporate both classes so that
I am trying use MySql and Entity Framework, using Connector/Net 6.1 with this as
I am trying use std::copy to copy from two different iterator. But during course
I'm trying use make a div's background transparent using a mixture of CSS3 rgba()
I'm trying use to selenium with firefox on CentOS from command line like this:
i'm trying use webview to load a image from sdcard i use this path

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.