How to set tcp_nodelay in GCDAsyncsocket?

我的未来我决定 提交于 2019-12-04 14:49:16

问题


Seems like the title is self descriptive. I want to increase the speed of sending and receiving data in my app and i was told to set tcp_nodelay to true. But i have no idea how to do that with GCDAsyncSocket. Can anyone help me?


回答1:


I haven't used GCDAsyncSocket, but "GCDAsyncSocket.h" shows that you can get the underlying socket descriptor with the socketFD method, which must be called only in a performBlock: call. So the following code might work:

[asyncSocket performBlock:^{
    int fd = [asyncSocket socketFD];
    int on = 1;
    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)) == -1) {
        /* handle error */
    }
}];

You might have to add

#include <netinet/tcp.h>
#include <netinet/in.h>

to your source file to compile this. As I said, I haven't tried this, but perhaps it helps to point you in the right direction.



来源:https://stackoverflow.com/questions/12007299/how-to-set-tcp-nodelay-in-gcdasyncsocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!