设置套接字选项以减少tcp段之间的时间延迟

时间:2018-12-01 06:37:58

标签: c++ c sockets networking tcpsocket

我正在使用套接字将数据发送到远程。数据有效载荷约为4kB。问题是tcp发送的tcp段太慢(每个tcp段延迟〜200-300ms)。
我尝试使用TCP_NODELAY(启用),TCP_QUICKACK(启用)和TCP_CORK(禁用),但无法减少延迟时间。
这是我的tcpdump,每个发送的4kB数据分为3个tcp段: tcpdump using my socket

但是,我看到了其他应用程序(与同一远程进行通信)的tcpdump。 tcp段之间的时间延迟非常小,甚至没有。 other app tcpdump 这是我的代码:

void createServerSocket() {
    int serverSockFd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
    std::string address = "fd53:7cb8:383:5::73";
    int port = 42519;

    if (serverSockFd < 0)
    {
        printf("Create Server socket fail");
        return;
    }
    struct sockaddr_in6 serverAddress;
    (void)memset(&serverAddress, 0, sizeof(sockaddr_in6));
    serverAddress.sin6_family = AF_INET6;
    serverAddress.sin6_port = htons(port);
    int result = inet_pton(AF_INET6, address.c_str(), &serverAddress.sin6_addr);

    if (result <= 0)
    {
        printf("inet_pton() failed portnumber: %d, address: %s \n", port, address.c_str());
        return;
    }

    // setting socket options
    int flag = 1;

    if(setsockopt(serverSockFd,IPPROTO_TCP,TCP_QUICKACK  ,(char *)&flag,sizeof(flag)) == -1)
    {
        printf("setsockopt TCP_QUICKACK failed for server socket on address %s \n", address.c_str());
    }
    if(setsockopt(serverSockFd,IPPROTO_TCP,TCP_CORK,(char *)&flag,sizeof(flag)) == -1)
    {
        printf("setsockopt TCP_CORK failed for server socket on address %s \n", address.c_str());
    }
    if(setsockopt(serverSockFd,IPPROTO_TCP,TCP_NODELAY,(char *)&flag,sizeof(flag)) == -1)
    {
        printf("setsockopt TCP_NODELAY failed for server socket on address %s \n", address.c_str());
    }

    result = bind(serverSockFd, (struct sockaddr*)&serverAddress, sizeof(sockaddr_in6));

    if (result != 0)
    {
        printf("bind() failed portnumber: %d, address: %s \n", port, address.c_str());
        return ;
    }

    result = listen(serverSockFd, 10);
    if (result != 0) {
        printf("listen() failed portnumber: %d, address: %s \n", port, address.c_str());
        return ;
    }

    acceptConnect(serverSockFd);
}

void acceptConnect(int serverSock)
{
    struct sockaddr_in6 clientAddress;
    socklen_t len = sizeof(sockaddr_in6);
    memset(&clientAddress, 0, sizeof(sockaddr_in6));

    const int clientSocket = accept(serverSock, (struct sockaddr*)&clientAddress, &len);

    if(clientSocket >= 0) {
        char str_addr[INET6_ADDRSTRLEN];
        inet_ntop(AF_INET6, &(clientAddress.sin6_addr),
                  str_addr, sizeof(str_addr));
        printf("New connection from: %s:%d ...\n", str_addr, ntohs(clientAddress.sin6_port));
        receiveDataFromSocket(clientSocket);
    }
}

void receiveDataFromSocket(int socket)
{
    int SOCKET_BUFFER_MAX_SIZE = 8*1024;

    char buffer[SOCKET_BUFFER_MAX_SIZE];
    memset(buffer, '\0', SOCKET_BUFFER_MAX_SIZE);
    //Receive data from sock
    while (true) {
        int dataLen = recv(socket, buffer, SOCKET_BUFFER_MAX_SIZE, 0);
        printf("Receive data from socket: %d, msgLength = %d\n", socket, dataLen);
        sendDataOverSocket(socket);
    }
}

void sendDataOverSocket(int clientSock)
{
    int dataLen = 4*1024 + 7;
    char *buf = new char[dataLen];
    memset(buf, 'a', 4*1024 + 7);
    int ret;
    ret = send(clientSock, buf, dataLen, 0);

    if (ret <= 0) {
        printf("ERROR Send message over socket");
        return;
    }

    int error_code;
    socklen_t error_code_size = sizeof(sockaddr_in6);
    getsockopt(clientSock, SOL_SOCKET, SO_ERROR, &error_code, &error_code_size);
    printf("Error code size: %d, error code: %d\n", error_code_size, error_code);
}

我的套接字设置是否不足以在第二张图片中达到tcpdump?

1 个答案:

答案 0 :(得分:-2)

我想您发送的数据传递时间太少。 尝试这个,在“ void sendDataOverSocket(int clientSock)”函数中,更改发送缓冲区: int dataLen = 4 * 1024 + 7; ===>更大。 然后尝试。