-2

我想以比特/秒为单位计算传输速率。我有一个变量来保存发送的总字节数,但我不知道三个是否是实现计时器的 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 回答