0

I am trying to communicate with a java server using NSStream however I can't determine how to limit the data sent by this code to a single "hello" and also prevent it from lopping off the initial 2 characters.

NSInputStream *inputStream;
NSOutputStream *outputStream;

-(void)initNetworkCommunication {
    NSLog(@"init net con");
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 6066, &readStream, &writeStream);
    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];
}

-(void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
    case NSStreamEventHasSpaceAvailable: {
        if (stream == outputStream) {
            NSString *res = @"<Hello>";
            NSData *data = [[NSData alloc] initWithData:[res dataUsingEncoding:NSUTF8StringEncoding]];
            [outputStream write:[data bytes] maxLength:[data length]];
            break;
        }
    }
}
Lorenzo
  • 3,293
  • 4
  • 29
  • 56

0 Answers0