2011-12-03 11 views
6

Estoy tratando simplemente de enviar y recibir un mensaje usando GCDAsyncSocket y no puedo hacer que funcione.No se llama al método didReadData GCDAsynSocketDelegate. Usando GCDAsynSocket

Estoy estableciendo con éxito la conexión y escribiendo mensajes, pero cuando se trata de leer, nunca se llama a mi delegado.

estoy im utilizando IOS5 y esta configuración:

Cliente:

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

} 

Delegado didReadData método no se llama

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

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

} 

servidor

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

} 

escribir un mensaje a c U Í ADE REFERENCIA y esperar la respuesta de conexión de socket éxito

- (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]; 

} 

Y esto no se llama siempre ...

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{ 
    NSLog(@"New message from client... "); 
} 

Respuesta

3

Ok, encontrado la respuesta.

El problema era que estaba escribiendo y leyendo en mi propio extremo de socket en lugar del socket conectado.

Fix: (cambiado asyncSocket a 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]; 

} 
Cuestiones relacionadas