开发者

how to disable Nagle algorithm on TCP connection on iPhone

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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜