Question

I am doing client and server that is able to send and receive files. But I can't really find any useful function that I can use to serve my purpose.

In client I tell the server which file to download and on which port and on the server I tell the port and limit on download speed in kB/s. I know how to do the connecting and stuff around it, but I really don't know how to choose the file, how to send it and how to receive it + how to set the speed limit on download. (I should use socket API and TCP protocol)

Was it helpful?

Solution

This is a something from where you can start.

Server

/*Receive File from Client */
#define LENGTH 512
    char* fr_name = "/file.jpg";
    FILE *fr = fopen( fr_name, "wb");
    if( fr == NULL)
        printf("File %s Cannot be opened file on server.\n", fr_name);
    else
    {
        bzero( revbuf, LENGTH); 
        int fr_block_sz = 0;

        bpt::ptime start, stop;
        start = bpt::microsec_clock::local_time();
        int totalBytes = 0;
        double transferRate = 0.0;

        while((fr_block_sz = recv( nsockfd, revbuf, LENGTH, 0)) > 0) 
        {
            stop = bpt::microsec_clock::local_time();
            bpt::time_duration dur = stop - start;
            double seconds = dur.total_milliseconds() / 1000.0;
            totalBytes += fr_block_sz;
            transferRate = totalBytes / seconds; // b/s

            int write_sz = fwrite( revbuf, sizeof(char), fr_block_sz, fr);
            if( write_sz < fr_block_sz)
            {
                error( "File write failed on server.\n");
            }
            bzero( revbuf, LENGTH);
            if ( fr_block_sz == 0 || fr_block_sz != 512) 
            {
                break;
            }
        }
        if(fr_block_sz < 0)
        {
            if (errno == EAGAIN)
            {
                printf("recv() timed out.\n");
            }
            else
            {
                fprintf(stderr, "recv() failed due to errno = %d\n", errno);
                exit(1);
            }
        }
        printf("Ok received from client!\n");
        fclose(fr); 
    }

Client

FILE *fs = fopen(fs_name, "rb");
    if(fs == NULL)
    {
        printf("ERROR: File %s not found.\n", fs_name);
        exit(1);
    }

    bzero(sdbuf, LENGTH); 
    int fs_block_sz;

    bpt::ptime start, stop;
    start = bpt::microsec_clock::local_time();
    int totalBytes = 0;
    double transferRate = 0.0;

    while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
    {
        if(send(sockfd, sdbuf, fs_block_sz, 0) < 0)
        {
            fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
            break;
        }
        stop = bpt::microsec_clock::local_time();
        bpt::time_duration dur = stop - start;
        double seconds = dur.total_milliseconds() / 1000.0;
        totalBytes += fs_block_sz;
        transferRate = totalBytes / seconds; // b/s

        bzero(sdbuf, LENGTH);
    }

Instead of boost you can use standard utilities from <time.h>

#include <time.h>
time_t start,end;
time (&start);
.
.
.
<your code>
.
.
.
time (&end);
double dif = difftime (end,start);
printf ("Elasped time is %.2lf seconds.", dif );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top