Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCDAsynSocketDelegate didReadData method is not called. Using GCDAsynSocket

I am trying to simply send and recieve a message using GCDAsyncSocket and cannot get it to work.

I am successfully establishing the connection and writing messages, but when it comes to reading my delegate is never called.

I am im using ios5 and this setup:

Client:

-(void) connectToHost:(HostAddress*)host{

    NSLog(@"Trying to connect to host %@", host.hostname);

    if (asyncSocket == nil)
    {
        asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

        NSError *err = nil;
        if ([asyncSocket connectToHost:host.hostname onPort:host.port error:&err])
        {
            NSLog(@"Connected to %@", host.hostname);

            NSString *welcomMessage = @"Hello from the client\r\n";
            [asyncSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];

            [asyncSocket readDataWithTimeout:-1 tag:0];
        }else
            NSLog(@"%@", err);
    }

}

Delegate didReadData method is not called

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

    NSLog(@"MESSAGE: %@", [NSString stringWithUTF8String:[data bytes]]);

}

Server

-(void)viewDidLoad{

    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    connectedSockets = [[NSMutableArray alloc] init];

    NSError *err = nil;
    if ([asyncSocket acceptOnPort:0 error:&err]){

        UInt16 port = [asyncSocket localPort];

        //...bojour stuff
    }
    else{
        NSLog(@"Error in acceptOnPort:error: -> %@", err);
    }

}

Write a message to client and wait for response on successful socket connection

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);

    // The newSocket automatically inherits its delegate & delegateQueue from its parent.

    [connectedSockets addObject:newSocket];

    NSString *welcomMessage = @"Hello from the server\r\n";
    [asyncSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];

    [asyncSocket readDataWithTimeout:-1 tag:0];

}

And this is not called ever...

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSLog(@"New message from client... ");
}
like image 361
Cyprian Avatar asked Dec 03 '11 14:12

Cyprian


1 Answers

Ok, found the answer.

The problem was that I was writing and reading on my own socket end instead of the connected socket.

Fix: (changed asyncSocket to newSocket)

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);

    // The newSocket automatically inherits its delegate & delegateQueue from its parent.

    [connectedSockets addObject:newSocket];

    NSString *welcomMessage = @"Hello from the server\r\n";
    [newSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];

    [newSocket readDataWithTimeout:-1 tag:0];

}
like image 84
Cyprian Avatar answered Oct 16 '22 09:10

Cyprian