打开套接字,发送数据,关闭套接字=截断数据

问题描述

|| 我正在从正在开发的iphone应用程序在套接字上发送数据时遇到麻烦。 简而言之,我想在特定端口上连接到TCP服务器,发送数据有效载荷,然后关闭套接字。 好像套接字没有在关闭前刷新等待在缓冲区中的数据。 这是我的代码
int port = 1234;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kcfAllocatorDefault,(CFStringRef)host,port,&readStream,&writeStream);
if (readStream && writeStream) {
    //CFReadStreamSetProperty(readStream,kcfStreamPropertyShouldCloseNativeSocket,kcfBooleanTrue);
    CFWriteStreamSetProperty(writeStream,kcfBooleanTrue);

    inputStream = (NSInputStream *)readStream;
    [inputStream retain];
    [inputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];

    outputStream = (NSOutputStream *)writeStream;
    [outputStream retain];
    [outputStream setDelegate:self];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream open];
}

[outputStream write:(void *)&len maxLength:4];
[outputStream write:[data bytes] maxLength:[data length]];

if (inputStream) {
    CFReadStreamClose((CFReadStreamRef)inputStream);
    CFRelease(inputStream);
}
if (outputStream) {
    CFWriteStreamClose((CFWriteStreamRef)outputStream);
    CFRelease(outputStream);
}   
感谢所有想法 詹斯     

解决方法

        您可以尝试关闭CFWriteStreamGetStatus返回kCFStreamStatusAtEnd,然后再关闭它。我认为问题在于CFWriteStreamClose会自动从使用该字符串的任何地方释放该字符串,包括实际将数据推送到网络的位置,这意味着在释放数据时它将未推送的数据留在缓冲区中。这应确保所有数据在刷新之前都已推出。 http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFWriteStreamRef/Reference/reference.html