-2

ビット/秒単位で転送速度を計算したい。送信された合計バイト数を保持する変数がありますが、3 がタイマーを実装する ac 関数であるかどうか、および while ループが送信中に実行されている時間を計算できるかどうかはわかりません。何か案は?

サーバ

int recv_file(int ax25_socket)
{

        const char* filename = FILE_NAME;
        int rval;
           char buf[4000];
           FILE *file = fopen(filename, "wb");
           if (!file)
           {
               printf("Can't open file for writing");
               return;
           }

           do
           {
               rval = read(ax25_socket, buf, sizeof(buf));
               if (rval < 0)
               {

                   printf("Can't read from socket");
                   fclose(file);
                   return;
               }

               if (rval == 0)
                   break;

               int off = 0;
               do
               {
                   int written = fwrite(&buf[off], 1, rval - off, file);
                   if (written < 1)
                   {
                       printf("Can't write to file");
                       fclose(file);
                       return;
                   }

                   off += written;
               }
               while (off < rval);
           }
           while(1);

           fclose(file);


}

クライアント

int send_file(int ax25_socket)
{
    char buf[1024];
        //const char* filename = FILE_NAME;
        FILE *file = fopen(FILE_NAME, "rb");
        if (!file)
        {
            printf("Can't open file for reading");
            return;
        }
        else
        {
            printf("Found file: %s\n",FILE_NAME);
        }
        while (!feof(file))
        {
            int rval = fread(buf, 1, sizeof(buf), file);
            if (rval < 1)
            {
                printf("Can't read from file");
                fclose(file);
                return 0;
            }
            else
            {
                printf("Reading %d bytes from file!\n",rval);
            }

            int off = 0;
            do
            {
                int sent = write(ax25_socket, &buf[off], rval - off);
                if (sent < 1)
                {
                    printf("Can't write to socket");
                    fclose(file);
                    return 0;
                }
                else
                {
                    printf("writing to socket %d bytes\n",sent);
                }

                off += sent;
            }
            while (off < rval);
        }

        fclose(file);
        return 1;

}
4

1 に答える 1

0

Windows の場合:

int recv_file(int ax25_socket)
{
   float start=0, end=0;

   float timeTakenCompress, timeTakenDecompress;

    ........
    ............

    // start timer
    start=clock();


    // sending started

              do
               {
                   int written = fwrite(&buf[off], 1, rval - off, file);
                   if (written < 1)
                   {
                       printf("Can't write to file");
                       fclose(file);
                       return;
                   }

                   off += written;
               }
               while (off < rval);
           }
           while(1);

    // sending complete

    end=clock();

    timeTaken = (end - start) / CLOCKS_PER_SEC;

    printf("time elapsed %5.5f \n", timeTaken );

    ......
}

ヘッダー ファイル:

#include <ctime>

Linux の場合:

int recv_file(int ax25_socket)
{
    timeval t1, t2;
    double elapsedTime;

    ........
    ............

    // start timer
    gettimeofday(&t1, NULL);


    // sending started

              do
               {
                   int written = fwrite(&buf[off], 1, rval - off, file);
                   if (written < 1)
                   {
                       printf("Can't write to file");
                       fclose(file);
                       return;
                   }

                   off += written;
               }
               while (off < rval);
           }
           while(1);

    // sending complete

    gettimeofday(&t2, NULL);

    // compute and print the elapsed time in millisec
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    printf("time elapsed %5.5f \n", elapsedTime);

    ......
}

必要なヘッダーファイル

#include <sys/time.h>
于 2013-10-14T20:00:09.667 に答える