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 😀
From the AsyncSocket.h file:
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?
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: