当前位置: 技术问答>linux和unix
怎样统计文件传输的时间?
来源: 互联网 发布时间:2015-04-21
本文导语: 已经有了如下一对server/client程序,client向server传送文件。 怎么样计算并写出文件的传输时间呢,是在server端写么,加点什么呢? 谢谢指教!! 假设tcp连接已经建立: client端: fp = fopen (argv [2], "rb"); if(fp == N...
已经有了如下一对server/client程序,client向server传送文件。
怎么样计算并写出文件的传输时间呢,是在server端写么,加点什么呢?
谢谢指教!!
假设tcp连接已经建立:
client端:
fp = fopen (argv [2], "rb");
if(fp == NULL)
{
fputs ("file not found!!rnn", stdout);
}
else
{
fputs ("file transmitting ...rn", stdout);
while (feof (fp) == 0)
{
len = fread (send_buf, 1, BLOCK_SIZE, fp);
send (s, send_buf, len, 0);
}
fclose(fp);
fputs ("file transmit ok!!rnn", stdout);
}
close (s);
server端:
i=0;
while (1)
{
if ((new_s = accept(s, NULL, NULL)) 0)
{
recv_len = recv (new_s, recv_buf, sizeof(recv_buf), 0 );
fwrite (recv_buf, 1, recv_len, fp);
}
fclose (fp);
fputs ("file received ok!!rnn", stdout);
close (new_s);
}
怎么样计算并写出文件的传输时间呢,是在server端写么,加点什么呢?
谢谢指教!!
假设tcp连接已经建立:
client端:
fp = fopen (argv [2], "rb");
if(fp == NULL)
{
fputs ("file not found!!rnn", stdout);
}
else
{
fputs ("file transmitting ...rn", stdout);
while (feof (fp) == 0)
{
len = fread (send_buf, 1, BLOCK_SIZE, fp);
send (s, send_buf, len, 0);
}
fclose(fp);
fputs ("file transmit ok!!rnn", stdout);
}
close (s);
server端:
i=0;
while (1)
{
if ((new_s = accept(s, NULL, NULL)) 0)
{
recv_len = recv (new_s, recv_buf, sizeof(recv_buf), 0 );
fwrite (recv_buf, 1, recv_len, fp);
}
fclose (fp);
fputs ("file received ok!!rnn", stdout);
close (new_s);
}
|
beginflg=0;
while (recv_len > 0)
{
recv_len = recv (new_s, recv_buf, sizeof(recv_buf), 0 );
if ( beginflg=0 ){
beginflg=1;
记录系统时间;
}
fwrite (recv_buf, 1, recv_len, fp);
}
再次记录系统时间;
两次时间相减,即为传输时间。
|
...
struct tms start, end;
times( &start );
/*
begin communication with server
*/
times( &end );
fprintf( stderr,"elapsed=[%4.2f,%4.2f]n",
(float)(end.tms_utime - start.tms_utime)/(float)CLK_TCK,
(float)(end.tms_stime -start.tms_stime)/(float)CLK_TCK );
...
struct tms start, end;
times( &start );
/*
begin communication with server
*/
times( &end );
fprintf( stderr,"elapsed=[%4.2f,%4.2f]n",
(float)(end.tms_utime - start.tms_utime)/(float)CLK_TCK,
(float)(end.tms_stime -start.tms_stime)/(float)CLK_TCK );
...