2011-12-03 20 views
6

Próbuję po prostu wysłać i odebrać wiadomość za pomocą GCDAsyncSocket i nie mogę go uruchomić.Metoda GCDAsynSocketDelegate didReadData nie została wywołana. Korzystanie z GCDAsynSocket

Udaje mi się nawiązać połączenie i pisać wiadomości, ale jeśli chodzi o czytanie, mój delegat nigdy nie jest wywoływany.

Jestem im przy iOS5 i tę konfigurację:

Klient:

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

} 

Delegat didReadData metoda nie nazywa

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

} 

Napisz wiadomość do c lient i czekać na odpowiedź na udane połączenie gniazda

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

} 

i to nie nazywa się kiedykolwiek ...

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

Odpowiedz

3

Ok, znalazłem odpowiedź.

Problem polegał na tym, że pisałem i czytałem na własnym gniazdku zamiast podłączonego gniazda.

Fix: (zmiana asyncSocket do 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]; 

} 
Powiązane problemy