4

I have created a TCP Socket connection in my appDelegate didFinishLaunchingWithOptions method. That was the easy part, and I have successfully connected to my server. I am having great difficulty with reading the data from the server in my View. I have been looking through tutorials on how to appropriately (step by step) read data using CocoaAsyncSocket, but I haven't come across anything useful.

This is my code from my appDelegate:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    [self connect];

    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 connect method, at the bottom of the appDelegate file:

- (void)connect 
{
    [socket connectToHost:@"9.5.3.3" onPort:11005 error:nil];
}

That was the easy part. I now need to read data from the server. I know some kind of NSData or NSMutableData object needs to be created for to take the value of the data I read from the server. I just have been very unsuccessful in finding any tutorial or documentation that points me in the right direction. There are several different read functions, some with different parameters, etc. If anyone could point me to a resource that goes over this in depth*(I am a newbie, after all =P)* I would really appreciate it -- Or if somebody knows of an easy way to accomplish this goal and wouldn't mind providing sample code here :D

This is the library I'm using: CocoaAsyncSocket. I'm using the library AsyncSocket.h and AsyncSocket.m

I've been stuck at this for hours, so any help would be great appreciated.

Thanks!

Nina
  • 1,579
  • 2
  • 21
  • 51
Skizz
  • 1,211
  • 5
  • 17
  • 28
  • We have common problem. Might help what I got. [here][1] [1]: http://stackoverflow.com/questions/8243469/how-to-separate-data-received-using-asyncsocket – Bren Oct 22 '12 at 16:25

1 Answers1

4

This should work:

- (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);
    }
}

You should also implement some other delegate methods, for example:

- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"error - disconnecting");
    //you'd probably want to start reconnecting procedure here...
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    NSLog(@"disconnected");
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"connected");
}

EDIT: if memory serves me right there is some documentation and also some examples available with the library.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • I really appreciate the speedy reply. I'm still pretty new and have been having a hard time grasping the concept of delegate methods. Would you mind terribly giving me a simple explanation? I'm used to C/C++ where you define your method and simply call it when you're ready to use it (ie, myMethod();). For some reason I haven't picked up on delegate methods -- more so on how they are invoked. Thanks so much!! – Skizz Jun 05 '12 at 19:05
  • That's why SO is here for :) I'm a bit in hurry at the moment and this is not an easy topic to explain. Alex Rozanski posted nice answer on this topic [here](http://stackoverflow.com/a/1045854/653513). Also check the docs he links in his answer. – Rok Jarc Jun 05 '12 at 19:12
  • Understood. I will look through this other answer. Thanks so much :) – Skizz Jun 05 '12 at 19:15
  • Nice explanation by rokjarc. I have a problem that in my app didReadData method not being called. Please help me out what is the problem. I got success to write data and send it to server but not able to read data which is sent by server. – Birju Jun 08 '16 at 11:07
  • @Birju: It's hard if not impossible to say without seeing the actual implementation/code. First guesses would be: a) typo in a delegate callback method name; b) delegate being deallocated before data is actually received - is it allocated as a local variable inside some method?; c) server-side error: data not actually sent from the server. – Rok Jarc Jun 08 '16 at 15:31
  • @rokjarc thank you for response but i want say you that neither there is a typo in delegate nor deallocated before data received and server also send data in proper way. This things actually working in Android application problem is with only iOS. – Birju Jun 09 '16 at 04:27
  • I understand: but it is really hard to say without seeing some code. In your place i would document the problem and open a question on this topic. The better you will describe what is going on (and what not) the better are chances we will solve this quickly. – Rok Jarc Jun 09 '16 at 08:28