UDS Dgram UDP sockets while(1) in server

UDS Dgram UDP sockets while(1) in server

Problem Description:

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,
    if (bytes < 0) {
        perror("recvfrom failed");
    fprintf(fp, "%s", buffer);
    bzero(buffer, 1024);

end = clock();
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Receive in %f secondsn", cpu_time_used);

Solution – 1

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:


#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, ...);


// 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)

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.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.