Tyler Petrochko
Tyler Petrochko

Reputation: 2641

Issue with objective C outputstream flushing?

Hello I'm having an issue with an NSOutputStream in Objective C. I've got a server running up on my computer and the iPhone emulator sends data to the server and the server should send it back. The only issue is, when I send it, It's not sending the text until I exit out of the Emulator, and only then does the server get the information.

NSString* toSend = chatField.text;
NSData* sendData = [[NSData alloc] initWithData:[toSend dataUsingEncoding:NSASCIIStringEncoding]];
[outStream write:[sendData bytes] maxLength:[sendData length]];
[chatField setText:@""];

I initialized it with

CFReadStreamRef reader;
CFWriteStreamRef writer;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ipField.text, 5000, &reader, &writer);
inStream = (NSInputStream *)reader;
outStream = (NSOutputStream *)writer;
[inStream setDelegate:self];
[outStream setDelegate:self];
[inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inStream open];
[outStream open];

It seems to me like I'm missing something akin to the Java .flush() method. Does anyone know the problem?

Thanks!

Upvotes: 1

Views: 1410

Answers (1)

user2461228
user2461228

Reputation: 26

At the end of the message you are sending to the server add "\r\n":

NSString* toSend = [NSString stringWithFormat:@"%@\r\n", chatField.text];

Upvotes: 1

Related Questions