简体   繁体   中英

Sending packets with UDP in C and measuring the elapsed time

I want the client to send the server an initial packet. If the server receives the packet, then it should send 11 packets back to the client. After the first packet is sent to the client by the server, I will start a timer. Then I will stop the timer after the other 10 packets arrive to the client. The code I have so far for the client side is as follows:

/************* 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;
}

The code I have so far for the server side is as follows:

/************* 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;
}

However, when I run these I obtain the following output:

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.

So, I suppose the server just sends 11 packets back to back, and the client receives the packets back to back also. However, I want the program to operate like this:

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

and so on. I cannot see my mistake in the code. Could anyone help me find the mistake please? Sorry for the looong post. Thanks in advance.

As comment states you should implement ACK (acknowledgement) packages for both sender and receiver side. So it should like this:

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

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

After this point the sequence continues...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM