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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:24:34+00:00 2026-06-05T07:24:34+00:00

I have created a TCP Socket connection in my appDelegate didFinishLaunchingWithOptions method. That was

  • 0

I have created a TCP Socket connection in my appDelegate didFinishLaunchingWithOptions method. That was the easy part, and I have successfully connected to my server. I am having great difficulty with reading the data from the server in my View. I have been looking through tutorials on how to appropriately (step by step) read data using CocoaAsyncSocket, but I haven’t come across anything useful.

This is my code from my appDelegate:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    [self connect];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[tekMatrixViewController alloc] initWithNibName:@"tekMatrixViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
} 

And here is my connect method, at the bottom of the appDelegate file:

- (void)connect 
{
    [socket connectToHost:@"9.5.3.3" onPort:11005 error:nil];
}

That was the easy part. I now need to read data from the server. I know some kind of NSData or NSMutableData object needs to be created for to take the value of the data I read from the server. I just have been very unsuccessful in finding any tutorial or documentation that points me in the right direction. There are several different read functions, some with different parameters, etc. If anyone could point me to a resource that goes over this in depth*(I am a newbie, after all =P)* I would really appreciate it — Or if somebody knows of an easy way to accomplish this goal and wouldn’t mind providing sample code here 😀

This is the library I’m using: CocoaAsyncSocket. I’m using the library AsyncSocket.h and AsyncSocket.m

I’ve been stuck at this for hours, so any help would be great appreciated.

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-05T07:24:35+00:00Added an answer on June 5, 2026 at 7:24 am

    This should work:

    - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
    {
        NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
        NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
        if(msg)
        {
            NSLog(@"RX:%@",msg);
        }
    }
    

    You should also implement some other delegate methods, for example:

    - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
    {
        NSLog(@"error - disconnecting");
        //you'd probably want to start reconnecting procedure here...
    }
    
    - (void)onSocketDidDisconnect:(AsyncSocket *)sock
    {
        NSLog(@"disconnected");
    }
    
    - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
    {
        NSLog(@"connected");
    }
    

    EDIT: if memory serves me right there is some documentation and also some examples available with the library.

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

Sidebar

Related Questions

I have a small method I use to disable my socket that listens for
I have a TCP connection , which client is PHP and server is C#
Java Socket Server I have a Java process that is creating a listener on
I have implemented an asynchronous TCP server that is spawned by another process. It
I am trying to write a socket server that forks for every connection. I
I have created a TCP Sockets connection using CocoaAsyncSocket and whenever I try to
I have created a singleton class whose job is to create a tcp socket
I have written a Python TCP/IP server for internal use, using win32serviceutil/py2exe to create
I have created custom listview in that have list of textview & list of
I have created a console application in C# and there is main method (static)

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.