How do I get the underly BSD socket in NSURLSessionStreamTask?

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) {
return;
}
 
  // 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?

Accepted Reply

Wonder if any properties in

NSURLSessionConfiguration
could achive similar effect?

No. The focus of

NSURLSession
is, as you might imagine, HTTP[S], so there are limited options for TCP-level configuration.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

NSURLSessionStreamTask
does not necessary use a socket, and thus there’s no guarantee that you’ll be able to extract a socket from a stream task. On iOS-based platforms (but not the Mac, at least for now)
NSURLSession
use the user space networking stack, and sockets are a kernel construct.

Why are you trying to get a socket here?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

> We would like to get the underlying bsd socket to disable the Nagel algorithm


Are you wanting to get the socket to set TCP_NODELAY to disable the Nagel algorithm?

megaton wrote:

Are you wanting to get he socket to set

TCP_NODELAY
to disable the Nagel algorithm?

Ah, yes, I just noticed that tag on the original post. If so, there’s no way to do this with stream task. My recommendation is that you use

NWConnection
, where you can disable Nagling with the
noDelay
property.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes, tcp_nodelay is something we want to use. Thanks for the information. Wonder if any properties in NSURLSessionConfiguration could achive similar effect?


Thanks.

Wonder if any properties in

NSURLSessionConfiguration
could achive similar effect?

No. The focus of

NSURLSession
is, as you might imagine, HTTP[S], so there are limited options for TCP-level configuration.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Appreciate the confirming.