Objective C 输出流刷新有问题吗?

发布于 2024-11-24 16:31:51 字数 984 浏览 2 评论 0原文

你好,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-12-01 16:31:51

在发送到服务器的消息末尾添加“\r\n”:

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

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

NSString* toSend = [NSString stringWithFormat:@"%@\r\n", chatField.text];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文