6 Replies
      Latest reply on Oct 14, 2019 1:09 PM by xpingc206
      xpingc206 Level 1 Level 1 (0 points)

        We are using NSURLSessionStreamTask for tcp communication. We would like to get the underlying bsd socket to disable the Nagel algorithm. The issue is we are always having a NULL socket handle (get kCFStreamPropertySocketNativeHandle property) from the NSInputStream/NSOutputStream captured from NSURLSessionStreamTask.


        Below are the steps how we get the raw socket handler:


        1. Create NSURLSessionStream by using

        NSURLSessionStreamTask *_streamTask = [some_session streamTaskWithHostName:_hostname port:_port];


        2. Capture the steams of the task by using:

        [_streamTask captureStreams]


        3. Open streams in the delegate "updateInputStream: outputStream:"


        - (void)updateInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream {
          if (inputStream == nil || outputStream == nil) {


          // Cache the stream so we can use later
          _inputStream = inputStream;
          _outputStream = outputStream;

          [_inputStream setDelegate:self];
          [_outputStream setDelegate:self];

          [_inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
          [_outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

          [_inputStream open];
          [_outputStream open];


        4. We can use the _inputStream and _outputStream to send/receive data to the remote host.5. However, when trying to get kCFStreamPropertySocketNativeHandle for this _inputStream/_outputStream, NULL is always returned.


        CFDataRef socketData = CFWriteStreamCopyProperty((__bridge CFWriteStreamRef)_outputStream), kCFStreamPropertySocketNativeHandle);


        I also tried with the _inputStream by using CFReadStreamCopyProperty, but still a NULL is returned.

        Any idea on why NULL is returned for the input/output streams captured from NSURLSesssionStreamTask?