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