2013-10-14 2 views
-2

Я хочу рассчитать скорость передачи в единицах бит/с. У меня есть переменная, в которой хранятся все отправленные байты, но я не знаю, является ли три функцией c, реализующей таймер, и если можно вычислить время цикла while, проходящего через отправку. есть идеи?вызов: рассчитать скорость передачи через гнездо UDP

сервер

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; 

} 
+2

Первый результат при поиске "[с] Таймер" здесь на StackOverflow: HTTP: //stackoverflow.com/questions/2150291 – us2012

ответ

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> 
Смежные вопросы