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!
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
does make no sense at all. remove that.
instead there should be code like following to connect
and than, the socket will call
socket:didConnectToHost:port:, that might look likeand 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.