问题描述
您好,我在Objective C中遇到NSOutputStream问题.我的计算机上正在运行服务器,iPhone仿真器将数据发送到服务器,服务器应将其发送回去.唯一的问题是,当我发送它时,直到我退出仿真器,它才发送文本,然后服务器才获取信息.
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:@""];
我用
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];
在我看来,我好像缺少类似于Java .flush()方法的东西.有人知道这个问题吗?
It seems to me like I'm missing something akin to the Java .flush() method. Does anyone know the problem?
谢谢!
推荐答案
在您发送到服务器的消息末尾添加"\ r \ n":
At the end of the message you are sending to the server add "\r\n":
NSString* toSend = [NSString stringWithFormat:@"%@\r\n", chatField.text];
这篇关于关于目标C输出流冲洗的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!