如何在iPhone上的TCP连接上禁用Nagle算法 [英] how to disable Nagle algorithm on TCP connection on iPhone

查看:159
本文介绍了如何在iPhone上的TCP连接上禁用Nagle算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建套接字,使用

I'm building a socket , using


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


我看到当我用myWriteStream发送消息时,它会将几条消息连接在一起,然后发送它们。
我认为这是因为Nagle算法,我想禁用它。
有没有人知道怎么做?

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?

推荐答案

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

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);

(对于复合文字技巧。)

这篇关于如何在iPhone上的TCP连接上禁用Nagle算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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