gpt4 book ai didi

c++ - 套接字通过send()发送的tcp数据包慢

转载 作者:行者123 更新时间:2023-11-30 19:28:56 26 4
gpt4 key购买 nike

我正在尝试编写一个使用 tcp 套接字与设备通信的 C/C++ 应用程序。我的应用程序充当服务器套接字。当从设备套接字接收到数据时,服务器使用 send() 函数响应设备:

ssize_t send(int sockfd, const void *buf, size_t len, int flags);

缓冲区长度 (~4kB) 大于 MTU(1500 字节)。 TCP 将其分成更小的包。我用wireshark抓包。我看到tcp数据包之间的时间延迟是0.2-0.3s。如果我发送大数据(~100Mb),则需要很长时间(几个小时)。
我尝试使用setsockopt启用TCP_NODELAY、TCP_QUICKACK和TCP_CORK,但仍然无法减少时间延迟。
你们能帮我解决这个问题吗?
-------------------
编辑:这是我的代码:

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

void createServerSocket();
void acceptConnect(int serverSock);
void receiveDataFromSocket(int socket);
void sendDataOverSocket(int clientSock);

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

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);
}

最佳答案

我认为您超出了 TCP 发送缓冲区,您正在顺序接收和发送,如果另一端从套接字读取数据的速度不够快,您将在每个 send() 操作中等待,直到发送缓冲区中有空间.

检查您发送的信息是否多于接收的信息,我怀疑recv()正在读取小块(因为MTU),但您在每个循环中发送大约4KB,因此您的recv/send调用没有正确平衡。

关于c++ - 套接字通过send()发送的tcp数据包慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53457310/

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