当前位置: 首页 > 知识库问答 >
问题:

为什么UDP客户端发送数据的时间比服务器接收数据的时间长?

萧琛
2023-03-14

看起来它不应该那样工作。我在发送第一个数据包(大小为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

共有1个答案

司徒高寒
2023-03-14

基准测试是谎言,特别是如果你使用如此短的时间,也有数据包丢失。

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

这意味着由于ARP查找造成的延迟,服务器接收第一个数据包的时间比客户端发送的时间晚得多。但是,在知道MAC之后,所有数据包都可以在没有进一步延迟的情况下交付,这样服务器将在客户端发送最后一个数据包后不久接收到它。由于开始时间较晚,结束时间大致相同,因此时间差较小,这可以解释您所看到的情况。

 类似资料:
  • 我正在尝试使用UDP在服务器(在公共IP上)和客户端(通过NAT)之间进行双向通信。我的逻辑是,如果服务器向IP和它接收数据包的端口发送一些数据,客户端仍然应该得到它,因为NAT最终会有将数据包发送到客户端的映射。 客户端有 2 个进程,一个用于发送数据包,另一个进程用于接收数据。服务器继续等待数据,如果它获得数据,它会将数据发送回从接收数据的端口和 IP。 客户代码如下: client_recv

  • 我正在Unity中制作一个游戏,我试图将数据从客户端发送到服务器并返回到客户端(试图保存实例),但当我收到数据并尝试将数据发送回客户端时,它表示udp客户端未连接。 它成功地将数据从我的Unity客户端发送到服务器,但一旦它到达那里,套接字就会断开连接,我就无法返回任何内容。正如你所看到的,我试图设置一些多播选项,但它似乎不能正常工作。 客户: 服务器: 因此,服务器中UdpClient的实例会保

  • 我想做的是: 为了学习游戏编程,我使用专用服务器和UDP创建了一个回声设置。专用服务器在不同的城市(也就是不在我的本地网络中)。 在我的本地计算机上,我有一个udp客户端和服务器(两个不同的程序)。当我第一次启动python服务器时,windows防火墙立即询问我是否要添加异常。在允许我的python服务器联网之后,我仍然没有收到任何消息。(客户- 我的问题是: 游戏如何解决这个问题?我没有为我想

  • 本文向大家介绍如何与服务器之间发送和接收JSON数据,包括了如何与服务器之间发送和接收JSON数据的使用技巧和注意事项,需要的朋友参考一下 JavaScript可以将网络请求发送到服务器并加载JSON。JS使用称为AJAX的东西来做到这一点。AJAX代表异步JavaScript和XML。JS具有一个API,可提取以将GET(接收)和POST(发送)信息发送到服务器。 您可以通过以下方式使用访存来获

  • 已移动:https://superuser.com/questions/782549/udp-client-sending-icmp-port-unreachable-when-receiveing-messages-from-the-ser 我有一个使用luasocket的udp客户端,基本上是这样做的(有几个抽象层,但这是正在发生的事情): 虽然在服务器的调试输出(到远程主机的ssh)中看到一