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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:26:57+00:00 2026-06-05T00:26:57+00:00

I have created a singleton class whose job is to create a tcp socket

  • 0

I have created a singleton class whose job is to create a tcp socket connection and keep it open infinitely while the application is running. I believe my singleton class looks as it’s supposed to (with help from other StackOverflow questions to get me there) but my problem is that I do not know WHEN or HOW to invoke my class object. I’m still pretty new to iOS development, so this is all pretty foreign to me.

  • My app has multiple views
  • I want to use my singleton class to create a tcp socket connection and keep the connection open at all times
  • I do not know WHERE to call/invoke my singleton class object
  • I do not know HOW to call/invoke my singleton class object
  • I am using the SmallSockets library

Here are my class files:

SocketConnection.h

#import <Foundation/Foundation.h>

@interface SocketConnection : NSObject
{

}

+ (SocketConnection *)getInstance;

@end

SocketConnection.m

#import "SocketConnection.h"
#import "imports.h"

static SocketConnection *sharedInstance = nil;

@implementation SocketConnection

- (id)init
{
    self = [super init];

    if (self) 
    {
        while(1)
        {
            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;
            }
        }
    }
    return self;
}

+ (SocketConnection *)getInstance
{
    @synchronized(self) 
    {
        if (sharedInstance == nil) 
        {
            sharedInstance = [[SocketConnection alloc] init];
        }
    }
    return sharedInstance;
}

@end 
  • 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-05T00:26:59+00:00Added an answer on June 5, 2026 at 12:26 am

    To use this Singleton, and the connection associated with it, you could simply call the getInstance method to get a reference to the connection that your trying to use. You would effectively do this instead of the typical alloc/init process. You can use the getInstance method from each of your views, and they will all maintain the same connection as you are intending.

    Also, because the way you create the Singleton, if you call alloc/init instead of getInstance, you WILL create a new connection instead of using the previous one.

    An example of how you might do this to create a reference within a view would be:

    @property (strong, nonatomic) SocketConnection * connection
    -(void) awakeFromNib
    {
        connection = [SocketConnection getInstance];
    }
    

    Code similar to this would cause all of your views to use the same connection, but WOULD NOT create the connection until you had a view onscreen that is using the connection. To create the connection at application startup, add this same ‘getInstance’ to one of your appDelegate methods.

    Also, because you are using a singleton, and this variable is never really released, you may want to consider adding methods to reinitialize, remove and generally manage the connection to the server.

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

Sidebar

Related Questions

I have created a singleton class to keep track of my data on my
Using FluentNHibernate in a web application, I've created a singleton SessionFactory class to have
Actually i have created an singleton class. Now my singleton class extends Activity, and
My app has a singleton class called CycleManager. I have created a sealed class
I want to have singleton class that its object is not statically created. having
I have a java singleton class that has my application settings. I used this
I have created a Registry class in .NET which is a singleton. Apparently this
I have a singleton class which I would like to be created using Spring's
I have created a Singleton for my class which contains an instance of MemcachedClient
Basically I have a class which an instance of is created via a Singleton

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.