如何在 iPhone 上的 TCP 连接上禁用 Nagle 算法

发布于 2024-09-08 00:26:05 字数 582 浏览 7 评论 0原文

我正在构建一个套接字,使用


CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
                                       (CFStringRef) yourHostAsNSString,
                                       yourPortAsInteger,
                                       &myReadStream,
                                       &myWriteStream);
and I see that when I send a messages with "myWriteStream" it concatenate few message together and then send them. I think it happens because of the Nagle algorithm and I want to disable it. Does any one knows how to do it?

I'm building a socket , using


CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
                                       (CFStringRef) yourHostAsNSString,
                                       yourPortAsInteger,
                                       &myReadStream,
                                       &myWriteStream);


and I see that when I send a messages with "myWriteStream" it concatenate few message together and then send them.
I think it happens because of the Nagle algorithm and I want to disable it.
Does any one knows how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2024-09-15 00:26:05

不保证这会解决您的问题,但如果您想禁用 Nagle 算法,您需要从流中获取本机套接字并调用 setsockopt

CFDataRef nativeSocket = CFWriteStreamCopyProperty(myWriteStream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle *sock = (CFSocketNativeHandle *)CFDataGetBytePtr(nativeSocket);
setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, &(int){ 1 }, sizeof(int));
CFRelease(nativeSocket);

(向 Mike Ash 大声喊出复合文字< /a> 技巧。)

No guarantee this will fix your problem, but if you want to disable the Nagle algorithm, you need to get the native socket from the stream and call setsockopt.

CFDataRef nativeSocket = CFWriteStreamCopyProperty(myWriteStream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle *sock = (CFSocketNativeHandle *)CFDataGetBytePtr(nativeSocket);
setsockopt(*sock, IPPROTO_TCP, TCP_NODELAY, &(int){ 1 }, sizeof(int));
CFRelease(nativeSocket);

(Shout out to Mike Ash for the compound literal trick.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文