首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS NSOutputStream在第一个签名后拆分数据

iOS NSOutputStream在第一个签名后拆分数据
EN

Stack Overflow用户
提问于 2015-10-14 09:53:28
回答 1查看 170关注 0票数 0

我们目前正在开发一个TLS-服务器-客户端应用程序。为了测试,客户端发送一个内容为"0001“的命令,服务器发送一个答案。

客户端通过NSOutputStream的写方法发送数据.当数据第一次发送时,它就完全到达了。当我第二次或第三次发送数据时,数据在第一个字节之后被拆分。服务器获得第一个"0“,而不是其余的"001”。我不知道为什么会分裂。

来文内容如下: ServerConnect 发送到服务器: 0001 从服务器接收:好的 发送到服务器:0 从服务器接收:错误 发送到服务器: 001 从服务器接收:错误 发送到服务器:0 从服务器接收:错误 发送到服务器: 001 从服务器接收:错误 ServerDisconnect

我希望有人能帮我这个忙。

代码语言:javascript
复制
- (void)initNetworkCommunication:(NSString*)ns_IP :(UInt32)ui_Port :(BOOL)b_ValidateCertificate
{

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)ns_IP, ui_Port, &readStream, &writeStream);

    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;

    [inputStream setDelegate:(id)self];
    [outputStream setDelegate:(id)self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                          [NSNumber numberWithBool:b_ValidateCertificate], kCFStreamSSLValidatesCertificateChain,
                          [NSNumber numberWithBool:YES], kCFStreamPropertyShouldCloseNativeSocket,
                          kCFNull,kCFStreamSSLPeerName,
                          kCFStreamSocketSecurityLevelSSLv3, kCFStreamSSLLevel,
                          nil];

    CFReadStreamSetProperty((CFReadStreamRef)inputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
    CFWriteStreamSetProperty((CFWriteStreamRef)outputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);

    [inputStream open];
    [outputStream open];   
}
代码语言:javascript
复制
- (BOOL) sendData:(NSString*)ns_SendData :(NSUInteger)nui_bufferLength
{
  NSUInteger nui_length = [ns_SendData length];
  NSUInteger nui_chunkSize = nui_bufferLength;
  NSUInteger nui_offset = 0;
  long l_Result;

  NSString *ns_response  = [NSString stringWithFormat:@"%@", ns_SendData];
  NSData *nd_data = [[NSData alloc] initWithData:[ns_response dataUsingEncoding:NSASCIIStringEncoding]];

  // a  none-ASCII-symbol exists
  if ([nd_data length] == 0)
    return FALSE;

  // split the data
  do {
    NSUInteger nui_thisChunkSize = nui_length - nui_offset > nui_chunkSize ? nui_chunkSize : nui_length - nui_offset;
    NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[nd_data bytes] + nui_offset
                                         length:nui_thisChunkSize
                                   freeWhenDone:NO];
    nui_offset += nui_thisChunkSize;

    // send data to server
    l_Result = [outputStream write:[chunk bytes] maxLength:[chunk length]];
    NSLog(@"%s - %lu", __PRETTY_FUNCTION__, l_Result);

    if (l_Result == -1) {
        NSLog(@"error sending data");
        return false;
    }

  } while (nui_offset < nui_length);

  return true;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-09 10:05:02

使用TLS 1.2解决了这一问题。见AppTechnical Note TN2287

代码语言:javascript
复制
    const void* keys[] = { kCFStreamSSLValidatesCertificateChain,   kCFStreamSSLLevel };
    // New CFString values available only on iOS 5:
    // (if these values are used on versions before iOS 5, then will
    //    default to max TLS 1.0, min SSLv3)
    // kCFStreamSocketSecurityLevelTLSv1_0SSLv3 configures max TLS 1.0, min SSLv3
    // (same as default behavior on versions before iOS 5).
    // kCFStreamSocketSecurityLevelTLSv1_0 configures to use only TLS 1.0.
    // kCFStreamSocketSecurityLevelTLSv1_1 configures to use only TLS 1.1.
    // kCFStreamSocketSecurityLevelTLSv1_2 configures to use only TLS 1.2.
    const void* values[] = { CFSTR("kCFBooleanTrue"), CFSTR("kCFStreamSocketSecurityLevelTLSv1_2") };

    CFDictionaryRef sslSettingsDict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 2,
                                                         &kCFTypeDictionaryKeyCallBacks,
                                                         &kCFTypeDictionaryValueCallBacks);

    CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, sslSettingsDict);
    CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, sslSettingsDict);
    CFRelease(sslSettingsDict);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33122138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档