如何在GCDAsyncsocket中设置tcp_nodelay? [英] How to set tcp_nodelay in GCDAsyncsocket?

查看:552
本文介绍了如何在GCDAsyncsocket中设置tcp_nodelay?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎标题是自我描述性的。我想提高在我的应用程序中发送和接收数据的速度,我被告知将tcp_nodelay设置为true。但我不知道如何使用GCDAsyncSocket。任何人都可以帮助我吗?

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?

推荐答案

我没有使用 GCDAsyncSocket ,但是GCDAsyncSocket.h表明您可以使用 socketFD 方法获取底层套接字描述符,该方法只能在 performBlock:调用中调用。因此,以下代码可能有效:

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 */
    }
}];

您可能需要添加

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

这篇关于如何在GCDAsyncsocket中设置tcp_nodelay?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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