Objective C 输出流刷新有问题吗?
你好,我在 Objective C 中遇到了 NSOutputStream 的问题。我的计算机上运行着一个服务器,iPhone 模拟器将数据发送到服务器,服务器应该将其发回。唯一的问题是,当我发送它时,它不会发送文本,直到我退出模拟器,然后服务器才会获取信息。
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() 方法的东西。有人知道问题所在吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在发送到服务器的消息末尾添加“\r\n”:
At the end of the message you are sending to the server add "\r\n":