gpt4 book ai didi

c - 为什么我的 UDP 客户端发送数据比服务器接收数据花费的时间更长?

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

看起来它不应该那样工作。我在发送第一个数据包(大小为 10 字节并一次线性增加 10 字节)后启动时钟(在客户端),并在发送最后一个数据包后将其关闭。

在服务器端(数据包的接收者),我在收到第一个数据包后启动时钟,并在收到最后一个数据包后停止。为什么发送比接收需要更长的时间?

客户端模型代码(UDP 发送方)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctime>

#define TRIALS 6500 // will have 6500 trials to keep under maximum UDP packet length of 65536
#define SERVERPORT 4950 // the port users will be connecting to

#define BUFFER 65001 // the max # of bytes I will be sending

int main(int argc, char *argv[])
{
clock_t start; // for calculating time
long double duration = 0; // final result of the time
unsigned long long int totalbytes = 0; // for calculating total data
unsigned long long int throughput = 0; // will be final result of the throughput (i.e. totalbytes/duration)

int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
int numbytes;

if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}

their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(SERVERPORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct

char message[BUFFER] = "abcdefghij"; //will start with 10 bytes

for (int i = 0; i < TRIALS; i++)
{
if ((numbytes = sendto(sockfd, message, strlen(message), 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
if (i == 0)
{
start = clock(); //startin clock after sending first message
}
strcat(message, "abcdefghij"); //will add 10 bytes at each try
//printf("Trial #: %d\n", i);
//printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
totalbytes += numbytes; //updating total number of bytes sent
}

duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
printf("\nThe process took %Lf seconds.\n", duration);
printf("A total of %llu bytes were sent.\n", totalbytes);
throughput = (long double)totalbytes/duration;
printf("The attempted data rate is: %llu bytes per second\n\n", throughput);

close(sockfd);
return 0;
}

以及UDP服务器(接收端)的代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctime>

#define MYPORT 4950 // the port users will be connecting to

#define MAXBUFLEN 65001 // max # of bytes I will be receiving.

int main(void)
{
int sockfd;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];

clock_t start;
long double duration = 0; // final result of the time
unsigned long long int totalbytes = 0; // for calculating total data
unsigned long long int throughput = 0; // will be final result of the throughput (i.e. totalbytes/duration)


if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

if (bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}

addr_len = sizeof(struct sockaddr);

int i = 0; // count number of packets received.
while(1)
{
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}

if(i == 0)
{
start = clock(); // starting time when receiving the first packet of length = 10 bytes.
}

//printf("Trial #: %d\n", i);
//printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
//printf("packet is %d bytes long\n",numbytes);
totalbytes += numbytes;
if(numbytes >= 65000) //if last packet lets break out of while loop
{
break;
}
//buf[numbytes] = '\0';
//printf("packet contains \"%s\"\n",buf);
++i;
}

duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
printf("\nThe process took %Lf seconds.\n", duration);
printf("A total of %llu bytes were received.\n", totalbytes);
throughput = (long double)totalbytes/duration;
printf("The actual data rate is: %llu bytes per second\n\n", throughput);

close(sockfd);
return 0;
}

编译运行服务器

user:socket$ g++ udp_server.cc -o udp_server

user:socket$ ./udp_server

编译运行服务器

user:socket$ g++ udp_client.cc -o udp_client

user:socket$ ./udp_client 127.0.0.1

示例结果:

客户端(发送方)

The process took 0.238971 seconds.

A total of 211282500 bytes were sent.

The attempted data rate is: 884134476 bytes per second

服务器(接收者)

The process took 0.042443 seconds.

A total of 211214670 bytes were received.

The actual data rate is: 4976431213 bytes per second

最佳答案

基准测试是谎言,尤其是当您使用如此短的时间并且还存在数据包丢失时。

但我的猜测是涉及 ARP 查找以查找服务器的 MAC 地址,因为这是将数据包传送到服务器网卡所必需的。只有在 ARP 响应之后,MAC 才已知,第一个数据包才能传送到服务器。一直以来,客户端已经发送了数据。其中一些位于发送缓冲区中,因为只要 MAC 未知(如果服务器和客户端在同一网络中),它们就无法发送。有些可能已经丢失,因为发送缓冲区已满。

这意味着由于 ARP 查找导致的延迟,服务器开始接收第一个数据包的时间比客户端发送的要晚得多。但是在 MAC 已知后,所有数据包都可以立即传送,这样服务器将在客户端发送后不久收到最后一个数据包。较晚的开始时间和(大约)相同的结束时间,时间差异较小,这可以解释您所看到的。

关于c - 为什么我的 UDP 客户端发送数据比服务器接收数据花费的时间更长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34172028/

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