Home > Mobile >  UDS Dgram \ UDP sockets while(1) in server
UDS Dgram \ UDP sockets while(1) in server

Time:12-07

I Want to measure how much time takes for each socket to transfer 100MB data file so I implement many of sockets type like TCP, some pipe's mmaps etc..

The process to measure is (on the client side )::= before I send the data I take time and after the data is sent I stop the time.

(On the Server side ) ::= before I receive the data I take time and after the file is written I take again time.

the problem is on the server side I never out from the while loop in UDP / UDS Dgram

so I never can measure the time it takes to transfer data between them.

For example here is half of my server the while part in UDP ipv6 server:

clock_t start = 0, end = 0;
FILE *fp;
char *filename = "new_data.txt";
fp = fopen(filename, "w");
/* now wait until we get a datagram */
printf("waiting for a datagram...\n");
clilen = sizeof(client_addr);
start = clock();

while (1) {
    ssize_t bytes = recvfrom(sock, buffer, 1024, 0,
                             (struct sockaddr *) &client_addr,
                             &clilen);
    if (bytes < 0) {
        perror("recvfrom failed");
        exit(4);
    }
    fprintf(fp, "%s", buffer);
    bzero(buffer, 1024);

}
close(sock);
//fclose(fp);
end = clock();
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Receive in %f seconds\n", cpu_time_used);

CodePudding user response:

One way to solve it would be to have an extra byte in your buffer that is a flag that indicates if there is any more data to come after this packet. Clear the flag when there is more data, set the flag when there is not. Something like:

Client:

#define CHUNK_SIZE 1024
char buffer[CHUNK_SIZE   1] = { 0 };

while ((num_bytes = fread(buffer   1, 1, CHUNK_SIZE, fp)) > 0)
{
    sendto(..., buffer, 1   num_bytes, ...);
}

buffer[0] = 1;
sendto(sock, buffer, 1, ...);

Server:

// Same chunk size and buffer as above

while ((num_bytes = recvfrom(sock, buffer, CHUNK_SIZE   1, ...)) > 0)
{
    if (num_bytes > 1)
    {
        fwrite(buffer   1, 1, num_bytes - 1, fp);
    }
    if (buffer[0] == 1)
        break;
}

There are other ways, such as having a TCP control socket running at the same time, over which you send out-of-band messages to indicate when the transmission is finished, but that is much more complex.

  • Related