How to set tcp_nodelay in GCDAsyncsocket?

2019-03-22 03:32发布

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条回答
迷人小祖宗
2楼-- · 2019-03-22 04:17

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.

查看更多
登录 后发表回答