有没有办法通过使用CFStreamCreatePairWithSocketToHost()来获取套接字引用? [英] Is there a way to get the socket reference by using CFStreamCreatePairWithSocketToHost()?

查看:127
本文介绍了有没有办法通过使用CFStreamCreatePairWithSocketToHost()来获取套接字引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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()来获取套接字引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆