GCDasyNsocketDelegate DidReadData non viene chiamato. Usando GCDasynSocket
-
27-10-2019 - |
Domanda
Sto cercando di inviare e ricevere semplicemente un messaggio usando GCDasyncsocket e non riesco a farlo funzionare.
Sto stabilendo con successo la connessione e la scrittura di messaggi, ma quando si tratta di leggere il mio delegato non viene mai chiamato.
Sto usando iOS5 e questa configurazione:
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);
}
}
Il metodo delegato didReadData non è chiamato
-(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);
}
}
Scrivi un messaggio al client e attendi la risposta sulla connessione socket riuscita
- (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];
}
E questo non si chiama mai ...
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSLog(@"New message from client... ");
}
Soluzione
Ok, ho trovato la risposta.
Il problema era che stavo scrivendo e leggendo sulla mia fine del mio socket invece della presa connessa.
Correzione: (modificato 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];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow