0

I use my app to receive data by using this method:

-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address withFilterContext:(id)filterContext {
  NSLog(@"didReceiveData");
  NSString* input = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSLog(@"%@", input);
  if ([input isEqual:@"LEDCube"]) {
        //do something
    }

Even though I send "LEDCube", the if statement always returns 0, that makes me unable to go further.

From NSLog, the content of input is "LEDCube". Therefore, I suspect that the problem is at the NSUTF8StringEncoding. How to deal with this problem?

Thank you in advance

  • 1
    I think your networking is fundamentally broken as you make the assumption that data will be delivered in the same chunks you sent them. That's not how it works and you need to construct an app-level *packet* and collect data until a complete packet is received and only *then* start parsing it. This normally means sending a *packet size* value followed by the payload. – trojanfoe Mar 11 '16 at 09:35
  • oh interesting. In my other program, I use sendto() from winsock in windows. So it means I need to send additional info from my other program? – Ghifari Rahadian Mar 11 '16 at 09:41
  • Yes. It will work fine most of the time but not every time. – trojanfoe Mar 11 '16 at 09:42
  • didReceiveData is often called multiple times as your data arrives bit by bit. You might get one didReceiveData with the characters "LEDC" followed by another one containing "ube". Log the output of data for a start. – gnasher729 Mar 11 '16 at 10:25

1 Answers1

-1

use isEqualToString instead of isEqual

if ([input isEqualToString:@"LEDCube"]) {
    //do something
}
S Patel
  • 36
  • 2