在C中使用UDP发送数据包并测量经过的时间



我希望客户端向服务器发送初始数据包。如果服务器接收到数据包,则应将11个数据包发送回客户端。服务器将第一个数据包发送到客户端后,我将启动一个计时器。然后,在其他10个数据包到达客户端后,我将停止计时器。我到目前为止为客户端拥有的代码如下:

/************* UDP CLIENT CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
int main(){
    int clientSocket, portNum, nBytes;
    char buffer[1500] = "q"; //changed this to 1500 from 1024
    struct sockaddr_in serverAddr;
    socklen_t addr_size;
    /*Create UDP socket*/
    clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
    /*Configure settings in address struct*/
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(7891);
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
    /*Initialize size variable to be used later on*/
    addr_size = sizeof serverAddr;
    printf("Sending initial.n");
    nBytes = 1500;
    sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
    nBytes = recvfrom(clientSocket,buffer,1500,0,NULL, NULL); // receive first time
    printf("Received first time.n");
    int k;
    // start timer
    time_t startc = time(NULL);
    printf("Clock started.n");
    // receive 10 more times
    for( k = 0; k < 10; k++){
        nBytes = recvfrom(clientSocket,buffer,1500,0,NULL, NULL);
        printf("Received %d times.n", (k+1));
    }
    // stop timer
    sleep(5);
    time_t endc = time(NULL);
    printf("Clock stopped.n");
    double seconds = ( (double)( endc - startc) ) / CLOCKS_PER_SEC;
    printf("Time elapsed: %fn", seconds);
    return 0;
}

我到目前为止服务器端的代码如下:

/************* UDP SERVER CODE *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(){
    int udpSocket, nBytes;
    char buffer[1500] = "q";    //changed this to 1500 from 1024
    struct sockaddr_in serverAddr, clientAddr;
    struct sockaddr_storage serverStorage;
    socklen_t addr_size, client_addr_size;
    int i;
    /*Create UDP socket*/
    udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
    /*Configure settings in address struct*/
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(7891);
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
    /*Bind socket with address struct*/
    bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
    /*Initialize size variable to be used later on*/
    addr_size = sizeof serverStorage;
    while(1){
        nBytes = recvfrom(udpSocket,buffer,1500,0,(struct sockaddr *)&serverStorage, &addr_size);
        // 11 times
        int j = 0;
        for( j = 0; j < 11; j++){
            /* Address and port of requesting client will be stored on serverStorage variable */
            /*Send uppercase message back to client, using serverStorage as the address*/
            sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
            //sleep(1);
            printf("Sent %d times.n", (j+1));
        }
    }
    return 0;
}

但是,当我运行这些时,我将获得以下输出:

Sending initial.
Sent 1 times.
Sent 2 times.
.
.
Sent 10 times.
Sent 11 times.
Received first time.
Clock started.
Received 1 times.
Received 2 times.
.
.
Received 10 times.
Clock stopped.
Time elapsed: 0.000005 seconds.

因此,我想服务器只是将11个数据包背回,而客户端也将数据包背回。但是,我希望该程序像这样运行:

Sending initial.
Received first time.
Clock started.
Sent 1 times.
Received 1 times.

等等。我看不到代码中的错误。谁能帮我找到这个错误?对不起,很宽敞的帖子。预先感谢。

作为注释指出,您应该为发件人和接收方侧面实现ACK(确认(软件包。因此应该这样做:

Sender:
Sending initial...
Waiting for ACK...
Receiver (receiver gets the package and send ACK):
Received first time.
Clock started.
Sending ACK to sender...

此后序列继续...

最新更新