iphoneobjective-ciosdelegatescocoaasyncsocket

Why aren't library (delegate) methods getting invoked? CocoaAsyncSocket


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 :D


Solution

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