1

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

Skizz
  • 1,211
  • 5
  • 17
  • 28
  • How is your windows server implemented? I'm asking because some 'servers' don't send data until their buffer is full or until they receive a CR&LF or CR charactes. So: are you absolutely certain that your server is sending the data? You can always check this by telneting to it. – Rok Jarc Jun 07 '12 at 19:59
  • Incidentally, if you want to understand how the delegate methods are invoked, look at the source code in AsyncSocket.m - lines 3782 to 3785 show exactly how onSocket:didReadData:withTag: is invoked. – glorifiedHacker Jun 07 '12 at 20:05
  • And another option is that your data is not UTF8 string. Try adding `NSLog ("RX length: %d", [data length]);` before `if(msg)`. What results do you get? Of course this will only make sense when you manage to get it invoked. – Rok Jarc Jun 07 '12 at 20:08
  • @rokjarc msg will either be something or nil, there is no third option, so his if-else block will execute one way or another. – glorifiedHacker Jun 07 '12 at 20:15
  • @glorifiedHacker: yes, `msg` will be something or nil. I'm talking about `data`. `msg` can be nil if `data` is not UTF8 coded - that doesn't mean the connection isn't working. – Rok Jarc Jun 07 '12 at 22:06
  • @Skizz: try that NSLog i mentioned before. You will get "Fail" if data isn't UTF8 encoded. What do you get for [data length]; – Rok Jarc Jun 07 '12 at 22:08
  • @rokjarc I re-read your comment and I misunderstood your meaning before. Sorry about that! – glorifiedHacker Jun 07 '12 at 22:25
  • No problem. I've seen exact same situation once, that why i think it might be (besides other things) UTF8 problem. – Rok Jarc Jun 08 '12 at 05:04
  • @rokjarc I tried the NSLog you mentioned, and this is what I get in my output: 2012-06-08 13:17:30.815 tekMatrix[2793:f803] RX length: 8 – Skizz Jun 08 '12 at 17:18
  • Ok, that means your connection is working and data can't be UTF8 encoded. You could try to printout data in hex format or something to see what you're actually receiving. A nice example on how to do that is here: http://stackoverflow.com/questions/1305225/best-way-to-serialize-a-nsdata-into-an-hexadeximal-string – Rok Jarc Jun 08 '12 at 17:22
  • Oh man. I already created a question similar to this. I will check out the SO post you've left here and try to come up with a resolution that way. Thanks for all the help and the speedy replies :) – Skizz Jun 08 '12 at 17:38

2 Answers2

5

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;
glorifiedHacker
  • 6,410
  • 2
  • 22
  • 26
  • Yes, willDisconnectWithError is implemented. I have tested this by disconnecting the ethernet cord cord from my laptop when I run my app in the simulator. I will update my original post with the additional delegate methods I have implemented. Thanks for the speedy reply! – Skizz Jun 07 '12 at 19:45
  • In all honesty, I'm not even sure how the didConnectToHost and didDisconnectWithError is being called/invoked. If my didReadData method is not being called/invoked, then how would I go about calling/invoking it? – Skizz Jun 07 '12 at 20:03
  • I've edited my answer above with more details from the AsyncSocket header file. Somewhere in your code, you should be calling something like: [socket readDataWithTimeout:10 tag:1234567890]; – glorifiedHacker Jun 07 '12 at 20:13
  • Awesome! My method is being called now. Albeit, my method is returning the "Fail" string in my output, but at least it is getting called. Thanks so much for the help and speedy replies! – Skizz Jun 07 '12 at 20:25
  • You're welcome. I would definitely recommend taking a look at the source code for AsyncSocket, as it will help you understand that there is nothing magical behind delegate methods - just cause and effect. – glorifiedHacker Jun 08 '12 at 05:05
0

I ran into a similar issue with the didReadData() method in my client app not being called after my server app sent set data via the [newSocket writeData:welcomData withTimout:-1 tag:1].

My problem was resolved once I inserted the following line within the didConnectToHost() method. [clientAsyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1.0 tag:0];

Now my client's didReadData() method is called appropriately whenever content is sent its way.

MarkMendy
  • 41
  • 1
  • 6