【问题标题】:Issue with objective C outputstream flushing?目标 C 输出流刷新问题?
【发布时间】:2011-07-14 14:50:55
【问题描述】:

您好,我在 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() 方法的东西。有谁知道这个问题?

谢谢!

【问题讨论】:

  • 你是如何创建/初始化你的流的?
  • 我编辑了它 ^ 并包含了创建/初始化

标签: iphone objective-c tcp flush outputstream


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多