问题描述
我正在使用这样的 CFStreamCreatePairWithSocketToHost
创建套接字 tcp 连接以获取写入流(我不想准备任何数据):
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);
这很有效,但由于 nagle 的算法,我的 tcp 命令被缓冲.这可能很酷,但在我的情况下,我需要尽快发送命令而没有任何延迟.
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?
推荐答案
CFDataRef socketData = CFWriteStreamCopyProperty(writeStream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle handle;
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), &handle);
// handle now contains the same thing as CFSocketGetNative(aSocket)
这篇关于有没有办法通过使用 CFStreamCreatePairWithSocketToHost() 来获取套接字引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!