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

The Archive Base Latest Questions

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

I have been stuck on this issue for a few days now and seem

  • 0

I have been stuck on this issue for a few days now and seem to be getting closer to a resolution (with help from the great users @ SO). I’m using the CocoaAsyncSocket library to create a TCP socket connection to a Windows server.

The connection is being made in my appDelegate:

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

I have a connect method (part of the library) called onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port:

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"connected :D");
}

This method is being called, as I can see the output from NSLOG and I am successfully connected. I can also see the connection is successful from the Windows machine (using log files).

I have also tested that another delegate method is being called as well:

- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"error - disconnecting");
    // start reconnecting procedure here...
}

I have tested that the willDisconnectWithError method works by running my app in the simulator and then unplugging my ethernet cord from my laptop. Once I did this, I saw in my output the “error – disconnecting” string.

The big problem is, however, is that my delegate method (again, from the library) is not being invoked.

Delegate method not being invoked:

- (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);
    }
    else 
    {
        NSLog(@"Fail");
    }    
}

I’m confident in my knowledge and understanding of how delegate methods work, but I still don’t quite follow HOW they’re INVOKED. To further complicate and cause confusion, one delegate method (onSocket: didConnectToHost port:) is being invoked, but the other (onSocket: didReadData tag:) is not being invoked. Unfortunately, this is only step one of my problem, but I’ll have to get this issue fixed up before I get to the other issue.

Any help would be greatly 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-05T10:40:23+00:00Added an answer on June 5, 2026 at 10:40 am

    From the AsyncSocket.h file:

    /**
     * Called when a socket has completed reading the requested data into memory.
     * Not called if there is an error.
    **/
    - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
    

    My first guess is that you have an error and that’s why your delegate method is not being called. Did you also implement the delegate method for handling errors?

    /**
     * In the event of an error, the socket is closed.
     * You may call "unreadData" during this call-back to get the last bit of data off the socket.
     * When connecting, this delegate method may be called
     * before"onSocket:didAcceptNewSocket:" or "onSocket:didConnectToHost:".
    **/
    - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err;
    

    NOTE: Just because you are able to connect without error, doesn’t mean that you are going to be able to read without an error occurring. The error argument that you pass into connectToHost:onPort:error: does not cover all error conditions.

    EDIT: Could you post the portion of code where you call one of the “readData” methods on the socket? There may be something that is being overlooked there. If for example, no “readData” message is being sent to the socket, then that would explain why your delegate method is not being called.

    EDIT: The onSocket:didReadData:withTag: method will only be called after you have invoked one of the following readData methods on the socket. For example:

    // The readData and writeData methods won't block (they are asynchronous).
    // 
    // When a read is complete the onSocket:didReadData:withTag: delegate method is called.
    // When a write is complete the onSocket:didWriteDataWithTag: delegate method is called.
    // 
    // You may optionally set a timeout for any read/write operation. (To not timeout, use a negative time interval.)
    // If a read/write opertion times out, the corresponding "onSocket:shouldTimeout..." delegate method
    // is called to optionally allow you to extend the timeout.
    // Upon a timeout, the "onSocket:willDisconnectWithError:" method is called, followed by "onSocketDidDisconnect".
    // 
    // The tag is for your convenience.
    // You can use it as an array index, step number, state id, pointer, etc.
    
    /**
     * Reads the first available bytes that become available on the socket.
     * 
     * If the timeout value is negative, the read operation will not use a timeout.
    **/
    - (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been stuck with this issue for a few days now, and really need,
I've been stuck on this for a few days now and can't seem to
so i have been stuck with this issue for 3 weeks now and i
I have been stuck on this for a while now and I cannot seem
I have been stuck with this issue now all morning. I actually saw this
I have been stuck on this for days, and was wondering if anyone had
I have now been stuck at this for some time so I though to
I have been stuck with this problem for a while now, and I just
I've been stuck with this problem from a couple of days and I can't
I've asked a few questions which have touched around this issue, but I've been

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.