我想以比特/秒为单位计算传输速率。 我有一个变量来保存发送的总字节数,但我不知道三个是否是一个实现定时器的c函数,以及是否可以计算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;
}
答案 0 :(得分: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>