1

I really need some help with my project... I need to exchange data with my server written in Java. I tried using GCDAsyncSocket, and I can send message to server, read it on server, but when server sends response to client, I can't (don't know how to) read it on client. Here is part of my code:

- (void) someMethod{
    NSError *err = nil;
    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    if(![asyncSocket connectToHost:@"localhost" onPort:7777 error:&err]){
        // If there was an error, it's likely something like "already connected" or "no delegate set"
        NSLog(@"I goofed: %@", err);
    }
    NSString *requestStr = @"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><root><service>1</service><type>1</type><userProperties><username>ivo</username></userProperties></root>";
    NSData *requestData = [requestStr dataUsingEncoding:NSUTF8StringEncoding];

    [asyncSocket writeData:requestData withTimeout:-1.0 tag:0];

    [asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:1.0 tag:0];
    [asyncSocket disconnectAfterWriting];
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
    if (tag == 0)
        NSLog(@"First request sent");
    else if (tag == 2)
        NSLog(@"Second request sent");
}

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);
}

Please help, if there is another way I am willing to try as I am getting desperate...

iblagajic
  • 71
  • 2
  • 9
  • Try looking at XMPP framework for example. It works rather good. https://github.com/robbiehanson/XMPPFramework – Roman Temchenko Dec 15 '11 at 20:38
  • Thanks for your answer, but I'm not sure that XMPP is what I need. If it is, can you point to example where I can see how it works with Java socket server? – iblagajic Dec 15 '11 at 21:26
  • I've been battling with this, and found it was my "server" code that was wrong causing the problem. Can you confirm that the server is working correctly using telnet ? Also, you're using the CRLFData terminator - Are you sure the server is sending a CR/LF combination to terminate the data beings sent ? – ferdil Dec 16 '11 at 01:01
  • I tested my server using client in Java. Isn't it like automatic if I use lineInputStream? What do I need at end of string? \r\n ??? – iblagajic Dec 16 '11 at 07:20
  • I tried adding EOL on my server and using [asyncsocket disconnectAfterReading] instead of [asyncsocket disconnectAfterWriting]. Again, client writes to server, but can't receive from server. – iblagajic Dec 16 '11 at 08:02

1 Answers1

0

I see that you're sending XML, with no particular terminator at the end of your request data, yet you're expecting the server to send a response terminated by a \r\n?

What does the protocol specify?

Sending and receiving data over tcp is a common cause of confusion because tcp is stream based. It has no concept of individual reads/writes. It treats all data as conceptually a never ending stream. The protocol dictates message boundaries. For a better explanation, see the "Common Pitfalls" article from GCDAsyncSocket's wiki: https://github.com/robbiehanson/CocoaAsyncSocket/wiki/CommonPitfalls

I think it will help explain a lot.

Robbie Hanson
  • 3,259
  • 1
  • 21
  • 16
  • can u pls help me out with this problem http://stackoverflow.com/questions/17631177/gcdasyncsocket-didreaddatawithtag-never-called-with-nsoperation-subclass – SandyNegi.037 Jul 14 '13 at 12:00