Is there a way to get the socket reference by usin

2019-02-14 00:51发布

问题:

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?

回答1:

CFDataRef socketData = CFWriteStreamCopyProperty(writeStream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle handle;
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), &handle);
// handle now contains the same thing as CFSocketGetNative(aSocket)