gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:14:49 29 4
gpt4 key购买 nike

我使用套接字将数据发送到远程。数据有效负载约为 4kB。问题是 tcp 发送的 tcp 段非常慢(每个 tcp 段延迟约 200-300 毫秒)。
我尝试了 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?

最佳答案

我认为您每次发送的数据太少。尝试这个,在“void sendDataOverSocket(int clientSock)”函数中,更改发送缓冲区:int dataLen = 4*1024 + 7; ===> 到一个更大的数字。然后尝试一下。

关于c++ - 设置套接字选项以减少 tcp 段之间的时间延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53568408/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com