Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the socket reference by using CFStreamCreatePairWithSocketToHost()?

I am creating a socket tcp connection using CFStreamCreatePairWithSocketToHost like this to get a write stream (I dont want to ready any data):

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)host, port, NULL, &writeStream);

This works pretty well but because of the nagle's algorithm my tcp commands are buffered. This may be cool but in my case I need to send the command as fast as possible without any delays.

I found a way to disable this "feature" using the following code found here:

int yes = 1;
setsockopt(CFSocketGetNative(aSocket), IPPROTO_TCP, TCP_NODELAY, (void *)&yes, sizeof(yes));

but I cant figure out how to get a valid reference to my socket. Can you help me?

like image 833
Thomas Kekeisen Avatar asked Nov 02 '10 22:11

Thomas Kekeisen


1 Answers

CFDataRef socketData = CFWriteStreamCopyProperty(writeStream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle handle;
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), &handle);
// handle now contains the same thing as CFSocketGetNative(aSocket)
like image 198
Lily Ballard Avatar answered Sep 28 '22 02:09

Lily Ballard