Client -Seitenanwendung funktioniert nicht wie beabsichtigt, wenn sie asyncsocket verwendet

StackOverflow https://stackoverflow.com/questions/1788228

  •  21-09-2019
  •  | 
  •  

Frage

Ich verwende eine Asyncsocket-Klasse in einer einfachen Client-Server-Anwendung. Als erster Schritt möchte ich das - sobald die Verbindung zwischen Client und Server hergestellt wird, übertragen Client eine Begrüßungsnachricht - "Angeschlossen mit XYZ Server" an Server und Server zeigt sie in textView an.

//The code in ClientController class is:

 -(void)awakeFromNib{
NSError *error = nil;
  if (![connectSocket connectToHost:@"192.168.0.32" onPort:25242 error:&error]) {
   NSLog(@"Error starting client: %@", error);
   return;
  }

  NSLog(@"xyz chat client started on port %hu",[connectSocket localPort]);
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
               [sock writeData:[@"connected to xyz server" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:30.0 tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
               // some relevant code goes here
}
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
               NSLog(@"within didWriteDataWithTag:"); // getting this message, means it should have written something to remote socket but
  // delegate- onSocket:didReadData:withTag: at server side is not getting invoked
}

// The code in ServerController class is:

 - (IBAction)startStop:(id)sender{
 NSLog(@"startStopAction");
 if(!isRunning)
 {
  NSError *error = nil;
  if(![listenSocket acceptOnPort:INPUT_PORT error:&error])
  {
   NSLog(@"Error starting server: %@", error);
   return;
  }

  NSLog(@"Echo server started on port %hu",[listenSocket localPort]);
  isRunning = YES;

  [sender setTitle:@"Stop"];
 }
 else
 {
  // Stop accepting connections
  [listenSocket disconnect];

  // Stop any client connections
  int i;
  for(i = 0; i < [connectedSockets count]; i++)
  {
   // Call disconnect on the socket,
   // which will invoke the onSocketDidDisconnect: method,
   // which will remove the socket from the list.
   [[connectedSockets objectAtIndex:i] disconnect];
  }

  NSLog(@"Stopped Echo server");
  isRunning = false;

  [sender setTitle:@"Start"];
 }
}
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
 [connectedSockets addObject:newSocket];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
 NSLog(@"Accepted client %@:%hu", host, port);  // it is getting displayed
 [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
        NSString *msgReceived = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
 NSLog(@"msgReceived in didReadData- %@",msgReceived); // it is not getting displayed
        [outputView insertText:msgReceived];
        [sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}

Kann mir jemand vorschlagen, wo ich mich irren kann? Danke im Voraus ...... Miraaj

War es hilfreich?

Lösung

Problem gelöst: Nur verwendet: [Sock ReadDatawithTimeout: Read_Timeout Tag: 0]; Anstelle von: [Sock ReadDatatodata: [Asyncsocket crlfdata] With Timeout: Read_Timeout Tag: 0];

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top