2011-12-03 25 views
6

Tôi đang cố gửi và nhận tin nhắn bằng cách sử dụng GCDAsyncSocket và không thể làm cho nó hoạt động.GCDAsynSocketDelegate didReadData method không được gọi. Sử dụng GCDAsynSocket

Tôi đang thiết lập thành công kết nối và viết tin nhắn, nhưng khi nói đến việc đọc đại biểu của tôi không bao giờ được gọi.

Tôi im sử dụng ios5 và thiết lập này:

Chủ đầu tư:

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

} 

Đại biểu didReadData phương pháp không được gọi

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

} 

Viết nhắn đến c lient và chờ đợi phản ứng về kết nối ổ cắm thành công

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

} 

Và điều này không được gọi bao giờ ...

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

Trả lời

3

Ok, tìm thấy câu trả lời.

Vấn đề là tôi đã viết và đọc trên đầu cắm của riêng mình thay vì ổ cắm được kết nối.

Fix: (thay đổi asyncSocket để 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]; 

} 
Các vấn đề liên quan