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!

Skizz
  • 1,211
  • 5
  • 17
  • 28

1 Answers1

3

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.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • What's DDLogError? Your own logging function? – Niklas Berglund Jun 06 '12 at 19:57
  • oh, no, it comes form [Lumberjack](https://github.com/robbiehanson/CocoaLumberjack), a collection of useful tools. – vikingosegundo Jun 06 '12 at 20:01
  • 1
    I haven't gone through StackOverflow's guidelines thoroughly, but I think it's better not to use 3rd party stuff in example code. NSLog() would keep it simple and is less likely to confuse newcomers. – Niklas Berglund Jun 06 '12 at 20:18
  • Cool. Didn't notice. Lumberjack seems cool though, will check it out :) – Niklas Berglund Jun 06 '12 at 20:19
  • @vikingosegundo I made some changes to my didFinishLaunchingWithOptions, but now my onSocket:socket didReadData:data withTag: method is not being invoked. Would you mind shedding some light on why that might be? – Skizz Jun 07 '12 at 19:07
  • Does the socket:didConnectToHost:port: get called ? – vikingosegundo Jun 07 '12 at 21:09
  • Yes, socket:didConnectToHost:port: is getting called. I see the NSLog string getting dumped to the output. I also ran a test to see if onSocket:willDisconnectWithError is getting called by running my app in the simulator and unplugging the ethernet cord, which successfully dumped the NSLog string to the output. I have created a new question for this specific issue. Thanks for all the help! – Skizz Jun 08 '12 at 15:20