gpt4 book ai didi

c - recvfrom 的延迟随它被调用的频率而变化

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:31 25 4
gpt4 key购买 nike

我不明白 recvfrom 在以下情况下的行为:我有一个发送和接收 udp 数据包并休眠的循环;根据该位接收 (recvfrom) 延迟会有所不同。如果我睡了 22 毫秒或更长时间,则延迟约为 170 微秒,如果我睡了 21 毫秒或更短时间,则延迟约为 1 毫秒。我错过了什么?为什么延迟变化如此之大?我考虑过中断合并,但我用 ethtool 检查了一下,它被禁用了。

代码来自https://gist.github.com/suyash/0f100b1518334fcf650bbefd54556df9并稍作修改。

客户端.c:

#include <arpa/inet.h> 
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <inttypes.h>

#define TIMESPEC_NSEC(ts) ((ts)->tv_sec * 1000000000ULL + (ts)->tv_nsec)

uint64_t realtime_now()
{
struct timespec now_ts;
clock_gettime(CLOCK_MONOTONIC, &now_ts);
return TIMESPEC_NSEC(&now_ts);
}

int main() {
const char* server_name = "192.168.1.240";
const int server_port = 8877;

struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
inet_pton(AF_INET, server_name, &server_address.sin_addr);
server_address.sin_port = htons(server_port);
int sock;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}

const char* data_to_send = "...";
struct timespec ts;
ts.tv_nsec = 21 *1000*1000;
ts.tv_sec = 0;
uint64_t t0,t1;
while(1)
{
int len =sendto(sock, data_to_send, strlen(data_to_send), 0,
(struct sockaddr*)&server_address,sizeof(server_address));

char buffer[100];
t0 = realtime_now();
recvfrom(sock, buffer, len, 0, NULL, NULL);
t1 = realtime_now();
printf("%" PRIu64 "\n",t1 - t0);
buffer[len] = '\0';
printf("recieved: '%s'\n", buffer);
int err = nanosleep(&ts, NULL);
if (err == EINTR)
{
err = 0;
}
if(err!=0)
{
perror("nanosleep failed");
break;
}
}
close(sock);
return 0;
}

服务器.c

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
int SERVER_PORT = 8877;
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);

int sock;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
printf("could not create socket\n");
return 1;
}

if ((bind(sock, (struct sockaddr *)&server_address,
sizeof(server_address))) < 0) {
printf("could not bind socket\n");
return 1;
}
struct sockaddr_in client_address;
socklen_t client_address_len = sizeof(client_address);
while (true) {
char buffer[100];
int len = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *)&client_address,
&client_address_len);

buffer[len] = '\0';
sendto(sock, buffer, len, 0, (struct sockaddr *)&client_address,
sizeof(client_address));
}

return 0;
}

最佳答案

请注意,您通过本地网络将数据包发送到 192.168.1.240(除非您设置了主机本地虚拟网络)。数据包首先通过您的交换机/路由器,然后到达您的服务器。反过来。因此,您在这里有效地测量了往返时间,而不是 recvfrom 的延迟。

关于c - recvfrom 的延迟随它被调用的频率而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51501261/

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