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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:43:39+00:00 2026-06-05T09:43:39+00:00

I have created a TCP Sockets connection using CocoaAsyncSocket and whenever I try to

  • 0

I have created a TCP Sockets connection using CocoaAsyncSocket and whenever I try to perform didReadData, I’m getting back blanks. I found the value of “msg” was @”” when I set break points and tried to debug.

This is what my appDelegate.m looks like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSData *data = nil;
    // Initialize socket object and make it a delegate.  Then call the delegate methods.
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    [self connect];
    [self onSocket:socket didConnectToHost:@"9.5.8.6" port:11005];
    [self onSocket:socket didReadData:data withTag:1]; // tags COULD be #defined *******

    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 onSocket:socket didReadData:data withTag: method:

- (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);
        if(msg == nil)
        {
            NSLog(@"msg is all Blanks");
        }
    } 
    else 
    {
        NSLog(@"Fail");
    }    
}

Note, this method is a method from the CocoaAsyncLibrary. I do not know if I’m invoking the method properly or if I’m not passing the correct arguments, or what.

When I run the app, all I see in my console is:

2012-06-06 11:44:00.434 tekMatrix[1378:f803] connected
2012-06-06 11:45:14.312 tekMatrix[1378:f803] RX:

Any and all help is very much appreciated.

Thanks!

EDIT

Here is what I have in my didFinishLaunchingWithOptions method now:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    NSError *error = nil;
    if (![socket connectToHost:@"199.5.83.63" onPort:11005 error:&error]) 
    {
        NSLog(@"Error connecting: %@", error);
    }

    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;
}

Now I’m able to see that I’m connected, but I’m still not understanding onSocket:socket didReadData:data withTag: method is not be invoked. Any help on this would be much 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-05T09:43:42+00:00Added an answer on June 5, 2026 at 9:43 am

    Sorry, I have to say: You got all about delegates wrong.

    You dont call the methods you implemented to fulfill the delegate’s protocol yourself — the delegator (in this case the socket) does that

    so this code

    [self connect];
    [self onSocket:socket didConnectToHost:@"9.5.8.6" port:11005];
    [self onSocket:socket didReadData:data withTag:1]
    

    does make no sense at all. remove that.

    instead there should be code like following to connect

    NSError *error = nil;
    if (![socket connectToHost:@"9.5.8.6" onPort:11005 error:&error]) {
        NSLog(@"Error connecting: %@", error);
    }
    

    and than, the socket will call socket:didConnectToHost:port:, that might look like

    -(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
    {
            [sock writeData:self.data withTimeout:-1 tag:1];
    
    }
    

    and the object socket will call the further delegate method implementations you provided.


    But as delegation is one very important pattern through out cocoa(-touch), ensure, you get it right.

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

Sidebar

Related Questions

I have a chat server in C/Linux using TCP sockets. When using libev I
I have created a TCP client using Apache Mina. I have added a while
I have created a TCP Socket connection in my appDelegate didFinishLaunchingWithOptions method. That was
I have written a Python TCP/IP server for internal use, using win32serviceutil/py2exe to create
i am using pcap to create a packet sniffer. i have this tcp structure:
I have a server in linux using the Berkeley_sockets and I create a TCP
I have a TCP connection , which client is PHP and server is C#
I am using the CocoaAsyncSockets library in order to create a tcp socket connection
I have created a singleton class whose job is to create a tcp socket
i am programing a client/server software in Python using sockets. I have a question,

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.