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

时间:2018-02-22 06:58:10

标签: c server udp client client-server

我希望客户端向服务器发送初始数据包。如果服务器收到数据包,那么它应该将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, '\0', 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: %f\n", 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, '\0', 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.

等等。我无法在代码中看到我的错误。有人能帮我找到错误吗?对不起,looong帖子。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如评论所述,您应该为发送方和接收方实施ACK(确认)包。所以它应该是这样的:

Sender:
Sending initial...
Waiting for ACK...

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

在此之后,序列继续......