I am trying to build a communicator ios client, I have a problem utilizing the socket connection using Foundation networking APIs; created socket is able to receive data fine however when attempting to send something, data is not send until ios application is terminated, at which point all earlier data is received by server. I am new to objective c, but tried to follow several tutorials and always encounter same issue.
I attach my code below:
- (void)viewDidLoad {
[super viewDidLoad];
[self openSocket];
}
- (void)openSocket {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)@"localhost", 1235, &readStream, &writeStream);
inputStream = (NSInputStream *)CFBridgingRelease(readStream);
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
outputStream = (NSOutputStream *)CFBridgingRelease(writeStream);
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@"stream event %lu", (unsigned long)streamEvent);
}
- (IBAction)sendMsg:(id)sender {
NSString *response = [NSString stringWithFormat:@"my test msg"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
}